[Tutor] Why doesn't it save the data before exiting?
Nathan Pinno
falcon3166 at hotmail.com
Tue Jun 27 00:48:03 CEST 2006
Hey all,
I am needing help on this. Why isn't it saving the data beore exiting the program? I don't get an error before exiting.
Here's the code so far:
accountlist = {}
def load_file(ac):
import os
import pickle
filename = 'accounts.txt'
if os.path.exists(filename):
store = open(filename, 'r')
ac = pickle.load(store)
else:
store = open(filename, 'w')
store.close()
def save_file(ac):
import pickle
store = open('accounts.txt', 'w')
pickle.dump(ac, store)
store.close()
def main_menu():
print "1) Add a new account"
print "2) Remove a account"
print "3) Print all info"
print "4) Find account"
print "5) Deposit"
print "6) Withdraw funds"
print "9) Save and exit."
def add():
print "Add a new account"
account = raw_input("Account Name: ")
amount = float(raw_input("Amount: "))
accountlist[account] = amount
def remove():
print "Remove a account"
account = raw_input("Account: ")
if accountlist.has_key(account):
del accountlist[account]
else:
print account," was not found."
def printall():
print "Account Info"
for account in accountlist.keys():
print account+"\t $"+str(accountlist[account]),"\n"
def lookup():
print "Specific Account Info"
account = raw_input("Account: ")
if accountlist.has_key(account):
print account+"\t $"+str(accountlist[account]),"\n"
else:
print account," was not found."
def deposit():
print "Deposit funds"
account = raw_input("Account: ")
if accountlist.has_key(account):
amount = float(raw_input("Amount: "))
accountlist[account] += amount
print account+"\t $"+str(accountlist[account]),"\n"
else:
print account," was not found."
def withdraw():
print "Withdraw Funds."
account = raw_input("Account: ")
if accountlist.has_key(account):
amount = float(raw_input("Amount: "))
accountlist[account] -= amount
print account+"\t $"+str(accountlist[account]),"\n"
else:
print account," was not found."
print "Account Tracker"
print "By Nathan Pinno"
print
load_file(accountlist)
while 1:
main_menu()
menu_choice = int(raw_input("Which item? "))
if menu_choice == 1:
add()
elif menu_choice == 2:
remove()
elif menu_choice == 3:
printall()
elif menu_choice == 4:
lookup()
elif menu_choice == 5:
deposit()
elif menu_choice == 6:
withdraw()
elif menu_choice == 9:
break
else:
print "That's not an option. Please choose a valid option."
save_file(accountlist)
print "Have a nice day!"
Thanks for the help so far!
Nathan Pinno
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060626/c104a468/attachment-0001.html
More information about the Tutor
mailing list