[Tutor] Needing help with my account tracker program.

Bob Gailer bgailer at alum.rpi.edu
Fri Jun 23 21:10:31 CEST 2006


Nathan Pinno wrote:
> Hey all,
>  
> I was wondering if you could help me fix a glitch in my program that's 
> preventing it from running.
>  
> Here's the code:
> accountlist = []
accountlist = {} # dictionary rather than list
>  
> def load_file(ac):
>     import os
>     filename = 'accounts.txt'
>     if os.path.exists(filename):
>         store = open(filename, 'r')
>         for line in store:
>             account = line.strip()
>             amount = line.next().strip()
>             ac[account] = amount
>     else:
>         store = open(filename, 'w') #create a new file
>     store.close
>  
> def save_file(ac):
>     store = open('accounts.txt', 'w')
>     for account, amount in accountlist.items():
>         store.write(account + '\n')
>         store.write(amount + '\n')
>     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 $",accountlist[account]+"\n"
>  
> def lookup():
>     print "Specific Account Info"
>     account = raw_input("Account: ")
>     if accountlist.has_key(account):
>         print account," $",accountlist[account]
>     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 $",accountlist[account]
>     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 $",accountlist[account]
>     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!"
> Here is the error I'm having troubles solving:
> Traceback (most recent call last):
>   File "C:\Python24\Account Tracker.py", line 87, in -toplevel-
>     add()
>   File "C:\Python24\Account Tracker.py", line 36, in add
>     accountlist[account] = amount
> TypeError: list indices must be integers
>  
> So. how do I solve it to make it work the way I want it to?
>  
> Thanks,
> Nathan Pinno
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>   


-- 
Bob Gailer
510-978-4454



More information about the Tutor mailing list