By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

Helps Please

P: 2
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(Statesinfo, f)
f.close()

f = open("pickles1.dat", "r")
Statesinfo = cPickle.load(f)
f.close()

choice = raw_input("\n\nChoice: ")
print

if choice == "0":
print "Good-bye."

elif choice == "1":
statecode = raw_input("Please 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(Statesinfo[statecode], f)
f.close()

f = open("pickles1.dat", "r")
Statesinfo[statecode] = cPickle.load(f)
f.close()
print Statesinfo

elif choice =="3":
statecode = raw_input("Which 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(Statesinfo[statecode], f)
f.close()

f = open("pickles1.dat", "r")
Statesinfo[statecode] = cPickle.load(f)
f.close()
print Statesinfo

elif choice =="4":
statecode = raw_input("Which 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(Statesinfo[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_tax = Statesinfo.values()
print allstatename_tax

elif choice == "7":
Statesinfo = Statesinfo.items()
print Statesinfo

elif choice =="8":
statecode = raw_input("Please 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_amount = statename_tax[2] * salesamount
print sales_tax_amount



else:
print "\nSorry, but", choice, "isn't a valid choice."

raw_input("\n\Press the enter key to exit.")
Nov 12 '06 #1
Share this Question
Share on Google+
1 Reply


bartonc
Expert 5K+
P: 6,596
Dear lostsoul,
You MUST use [code] tags. Experts are here to help, but don't have the time to reformat you code each time that you ask for help.
The gray lettering on the background where you post has instructions. So does the panel on your right (as you type in your post) called "Posting Guidelines". So does the sticky at the top of this forum called "Posting Guidelines". Read any or all of these, then post again USING CODE TAGS. thank you,
forum moderator
Nov 12 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.