2016-11-10 146 views
0
import random 

def balance(name): 
    return sum(transactions[name]) 

def available_credit(account_limit, name): 
    return account_limit-balance(name) 

def check_availability(new_transaction, account_limit, name): 
    if new_transaction>=available_credit(account_limit, name): 
     return False 
     """false if the new transaction amount is greater than what is 
     available""" 
    elif new_transaction<=available_credit(account_limit, name): 
     return True 
     """true if the new transaction amount is less than what is 
     available""" 


def make_transaction(new_transaction, account_limit, name): 
    if check_availability(new_transaction, account_limit, name)==True: 
     transactions[name].append(new_transaction) 
     return transactions[name] 
    elif check_availability(new_transaction, account_limit, name)==False: 
     return transactions[name] 

def statement(account_limit, name): 
    print "Balance:", balance(name) 
    print "Available credit:", available_credit(account_limit, name) 
    print "Transactions:" 
    for i in transactions[name]: 
     print i 
    transactions[name]=[] 


def main(): 
    limits={} 
    transactions={} 
    while 1==1: 
     """the menu will continue to ask the user the questions of this menu until the user ends the program""" 
     print "What would you like to do?" 
     print "1. Create New Account" 
     print "2. Make a new transaction" 
     print "3. Check your statement" 
     print "4. Quit" 
     choice=raw_input("Your choice:") 
     if choice!='1' and choice!='2'and choice!='3' and choice!='4': 
      """ if the choice is not 1 or 2 it conintues to ask the 
     choice untill the user inputs a valid option""" 
      while choice!='1' and choice!='2'and choice!='3' and choice!='4': 
       print "Invalid choice!" 
       choice=raw_input("Your choice:") 
     if choice=='1': 
      name=raw_input("Account name:") 
      account_limit=random.randint(500,50000) 
      print "New account created for", name+".", "Limit:", "$"+str(account_limit) 
      limits[name]=account_limit 
      transactions[name]=[] 
      print "" 
     if choice=='2': 
      name=raw_input("Which account?") 
      if name not in limits: 
       while name not in limits: 
        print "Invalid account name!", name, "does not exist." 
        name=raw_input("Which account?") 
      new_transaction=input("How much is the transaction?") 
      if new_transaction<=0: 
       while new_transaction<=0: 
        print "Invalid transaction! Must be a positive number." 
        new_transaction=input("How much is the transaction?") 
       if check_availability(new_transaction, account_limit, name)==True: 
        """this checks if the transaction can be made with the credit limit""" 
        make_transaction(new_transaction, account_limit, name) 
        print "Successful! Your balance is", balance(name),  "available credit is", available_credit(account_limit, name) 
        print "" 
       elif check_availability(new_transaction, account_limit, name)==False: 
        print "Transaction rejected. Your available credit is", available_credit(account_limit, name) 
        print "" 
      else: 
       if check_availability(new_transaction, account_limit, name)==True: 
        """this checks if the transaction can be made with the credit limit""" 
        make_transaction(new_transaction, account_limit, name) 
        print "Success! Your balance is", balance(name), "available credit is", available_credit(account_limit, name) 
        print "" 
       elif check_availability(new_transaction, account_limit, name)==False: 
        print "Transaction rejected. Your available credit is", available_credit(account_limit, name) 
        print "" 
     elif choice=='3': 
      """this returns the statement which also clears the 
     history of transactions""" 
      print "" #these empty prints throughout are to create spaces  
      statement(account_limit, name) 
      print "" 

     elif choice=='4': 
      break 

這是我得到的錯誤:我做錯了什麼?向詞典中添加鍵和值

What would you like to do? 
1. Create New Account 
2. Make a new transaction 
3. Check your statement 
4. Quit 
Your choice: 1 
Account name: james 
New account created for james. Limit: $2245 

What would you like to do? 
1. Create New Account 
2. Make a new transaction 
3. Check your statement 
4. Quit 
Your choice: 2 
Which account? james 
How much is the transaction? 200 
Traceback (most recent call last): 
    File "python", line 1, in <module> 
    File "python", line 80, in main 
    File "python", line 10, in check_availability 
    File "python", line 7, in available_credit 
    File "python", line 4, in balance 
NameError: global name 'transactions' is not defined 
+0

TL; DR - 這是很多代碼!它很好地歸結爲一個簡單的例子,它有相同的問題。 – tdelaney

回答

2

可變transactions是局部的main,這意味着只有代碼的main內可以訪問它,要麼使其全球性的,通過定義它外部的功能,或者將其傳遞作爲參數的函數需要它。

0

您是否嘗試過將變量事務傳遞給函數?