[Tutor] Why doesn't it save the data before exiting? CORRECTION

Bob Gailer bgailer at alum.rpi.edu
Tue Jun 27 01:33:31 CEST 2006


Nathan Pinno wrote:
> How do I know? Simple. Next time I load it and ask it to print the 
> list of accounts and how much in each, I only get:
>
> Account Info
>
> then the menu again, with no info.
Ah. But the program starts setting accountlist = {}. When you reach 
printall, accountlist is still {}. Can you figure out why?

Hint: load_file(accountlist) does not change accountlist.
>
> Nathan Pinno
> ----- Original Message ----- From: "Bob Gailer" <bgailer at alum.rpi.edu>
> To: "Bob Gailer" <bgailer at alum.rpi.edu>
> Cc: "Nathan Pinno" <falcon3166 at hotmail.com>; <tutor at python.org>
> Sent: Monday, June 26, 2006 5:19 PM
> Subject: Re: [Tutor] Why doesn't it save the data before exiting? 
> CORRECTION
>
>
>> Bob Gailer wrote:
>>> Nathan Pinno wrote:
>>>
>>>> Hey all,
>>>>  I am needing help on this. Why isn't it saving the data beore 
>>>> exiting the program?
>>>>
>>> But it does save it. What evidence do you have that it is not?
>>>
>>> Please in the future always tell us what the evidence of a problem is.
>>>
>>> Also I suggest you add validation of user input, to avoid the 
>>> program terminating if the user hits the wrong key. In fact the 
>>> whole menu thing would be easier to manage if the choices were 
>>> character rather than integer. Then you don't need int() conversion 
>>> and the exception raising if the user does not enter an integer string.
>>>
>>> Similar comment regarding checking input before applying float().
>>>
>>> Consider % formatting for the outputs as in:
>>>         print "%s\t $%2f\n" % (account, accountlist[account]) # 
>>> instead of
>>>         print account+"\t $"+str(accountlist[account]),"\n"
>>>
>>> Also I suggest you not open store for writing until just before the 
>>> pickle.dump. Otherwise it is possible to have an empty file on which 
>>> pickle.load will raise an exception.
>>>
>>>> I don't get an error before exiting.
>>>>
>>> Good. You should not, unless you enter something that won't convert 
>>> to integer, or string [ I MEANT float ], or you leave an empty 
>>> account.txt file.
>>>
>>>>  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
>>>> ------------------------------------------------------------
>>
>


-- 
Bob Gailer
510-978-4454



More information about the Tutor mailing list