2016-11-10 155 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

您是否尝试过将变量事务传递给函数?