Can someone give me some pointer on the below program? There are three things that I couldn't do with my current program. 1) Whenever I make changes to the dictionary it doesn't save the changes. 2) I don't know how to delete things using pickle method. 3) I am trying to get sales tax % only from the dictionary to figure out the sales tax amount.
Thanks in advance for any guidance.
import cPickle
choice = None
while choice != "0":
print \
"""
State name and sales tax percentage
0 - Quit
1 - Look Up a state name and sales tax percentage
2 - Add a state and its sales tax percentage
3 - Replace a state and its sales tax percentage
4 - Delete a state and its sales tax percentage
5 - List of all state codes
6 - List of all state names and sales tax percentage
7 - List of all Statesinfo
8 - Calculate sales tax amount and output state name
"""
Statesinfo ={"MN" : "Minnesota" "-" "6.5%","CA" : "California " "-" "7.25%", "FL" : "Florida" "-" "6%"}
Tax_percentage ={"MN":"6.5%" , "CA":"7.25% ", "FL":"6%"}
f = open("pickles1. dat", "w")
cPickle.dump(St atesinfo, f)
f.close()
f = open("pickles1. dat", "r")
Statesinfo = cPickle.load(f)
f.close()
choice = raw_input("\n\n Choice: ")
print
if choice == "0":
print "Good-bye."
elif choice == "1":
statecode = raw_input("Plea se enter the state code that you want to look up! ")
if statecode in Statesinfo:
statename_tax = Statesinfo[statecode]
print "\nHere is the state name and its sales tax percentage that you want to look up.",statename_ tax
else:
print "\nSorry, the state you want to look up is not on the list."
elif choice =="2":
statecode = raw_input("What is the state code?: ")
if statecode not in Statesinfo:
statename_tax = raw_input("What is the state name and sales tax percentage that you want to add: ")
Statesinfo[statecode] = statename_tax
print "\nAn additional state name and its sales tax percentage has been added starting with its state code.", statecode
else:
print "\nThat state already exists! Please add a different state code."
f = open("pickles1. dat", "w")
cPickle.dump(St atesinfo[statecode], f)
f.close()
f = open("pickles1. dat", "r")
Statesinfo[statecode] = cPickle.load(f)
f.close()
print Statesinfo
elif choice =="3":
statecode = raw_input("Whic h state do you want to change starting with the statecode?: ")
if statecode in Statesinfo:
statename_tax = raw_input("What is the replacement statename and tax percentage?: ")
Statesinfo[statecode] = statename_tax
print "\nThe new state name and tax percentage has changed for", statecode
else:
print "\nThat statecode doesn't exists!"
f = open("pickles1. dat", "w")
cPickle.dump(St atesinfo[statecode], f)
f.close()
f = open("pickles1. dat", "r")
Statesinfo[statecode] = cPickle.load(f)
f.close()
print Statesinfo
elif choice =="4":
statecode = raw_input("Whic h state do you want to delete starting with the statecode?: ")
if statecode in Statesinfo:
del Statesinfo[statecode]
print "\nOkay. I deleted the state name and tax percentage starting with the statecode's name", statecode
else:
print "\nI can't do that!", statecode , "doesn't exist in the dictionary."
f = open("pickles1. dat", "w")
cPickle.dump(St atesinfo[statecode], f)
f.close()
f = open("pickles1. dat", "r")
Statesinfo[statecode] = cPickle.load(f)
f.close()
print Statesinfo
elif choice == "5":
allstatecode = Statesinfo.keys ()
print allstatecode
elif choice == "6":
allstatename_ta x = Statesinfo.valu es()
print allstatename_ta x
elif choice == "7":
Statesinfo = Statesinfo.item s()
print Statesinfo
elif choice =="8":
statecode = raw_input("Plea se enter the state code that you want to calculate your sax tax amount? ")
statename_tax = Statesinfo[statecode]
print statename_tax
salesamount = raw_input("What is the sales amount? $ ")
sales_tax_amoun t = statename_tax[2] * salesamount
print sales_tax_amoun t
else:
print "\nSorry, but", choice, "isn't a valid choice."
raw_input("\n\P ress the enter key to exit.")
Thanks in advance for any guidance.
import cPickle
choice = None
while choice != "0":
print \
"""
State name and sales tax percentage
0 - Quit
1 - Look Up a state name and sales tax percentage
2 - Add a state and its sales tax percentage
3 - Replace a state and its sales tax percentage
4 - Delete a state and its sales tax percentage
5 - List of all state codes
6 - List of all state names and sales tax percentage
7 - List of all Statesinfo
8 - Calculate sales tax amount and output state name
"""
Statesinfo ={"MN" : "Minnesota" "-" "6.5%","CA" : "California " "-" "7.25%", "FL" : "Florida" "-" "6%"}
Tax_percentage ={"MN":"6.5%" , "CA":"7.25% ", "FL":"6%"}
f = open("pickles1. dat", "w")
cPickle.dump(St atesinfo, f)
f.close()
f = open("pickles1. dat", "r")
Statesinfo = cPickle.load(f)
f.close()
choice = raw_input("\n\n Choice: ")
if choice == "0":
print "Good-bye."
elif choice == "1":
statecode = raw_input("Plea se enter the state code that you want to look up! ")
if statecode in Statesinfo:
statename_tax = Statesinfo[statecode]
print "\nHere is the state name and its sales tax percentage that you want to look up.",statename_ tax
else:
print "\nSorry, the state you want to look up is not on the list."
elif choice =="2":
statecode = raw_input("What is the state code?: ")
if statecode not in Statesinfo:
statename_tax = raw_input("What is the state name and sales tax percentage that you want to add: ")
Statesinfo[statecode] = statename_tax
print "\nAn additional state name and its sales tax percentage has been added starting with its state code.", statecode
else:
print "\nThat state already exists! Please add a different state code."
f = open("pickles1. dat", "w")
cPickle.dump(St atesinfo[statecode], f)
f.close()
f = open("pickles1. dat", "r")
Statesinfo[statecode] = cPickle.load(f)
f.close()
print Statesinfo
elif choice =="3":
statecode = raw_input("Whic h state do you want to change starting with the statecode?: ")
if statecode in Statesinfo:
statename_tax = raw_input("What is the replacement statename and tax percentage?: ")
Statesinfo[statecode] = statename_tax
print "\nThe new state name and tax percentage has changed for", statecode
else:
print "\nThat statecode doesn't exists!"
f = open("pickles1. dat", "w")
cPickle.dump(St atesinfo[statecode], f)
f.close()
f = open("pickles1. dat", "r")
Statesinfo[statecode] = cPickle.load(f)
f.close()
print Statesinfo
elif choice =="4":
statecode = raw_input("Whic h state do you want to delete starting with the statecode?: ")
if statecode in Statesinfo:
del Statesinfo[statecode]
print "\nOkay. I deleted the state name and tax percentage starting with the statecode's name", statecode
else:
print "\nI can't do that!", statecode , "doesn't exist in the dictionary."
f = open("pickles1. dat", "w")
cPickle.dump(St atesinfo[statecode], f)
f.close()
f = open("pickles1. dat", "r")
Statesinfo[statecode] = cPickle.load(f)
f.close()
print Statesinfo
elif choice == "5":
allstatecode = Statesinfo.keys ()
print allstatecode
elif choice == "6":
allstatename_ta x = Statesinfo.valu es()
print allstatename_ta x
elif choice == "7":
Statesinfo = Statesinfo.item s()
print Statesinfo
elif choice =="8":
statecode = raw_input("Plea se enter the state code that you want to calculate your sax tax amount? ")
statename_tax = Statesinfo[statecode]
print statename_tax
salesamount = raw_input("What is the sales amount? $ ")
sales_tax_amoun t = statename_tax[2] * salesamount
print sales_tax_amoun t
else:
print "\nSorry, but", choice, "isn't a valid choice."
raw_input("\n\P ress the enter key to exit.")
Comment