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

変数transactionsmainの内部コードのみのいずれか、それをアクセスする機能の外、それを定義することによって、それがグローバルする、またはその関数の引数として渡すことができることを意味する、mainに対してローカルでありますそれが必要。

0

あなたは機能するために可変トランザクションを渡そうとしましたか?

関連する問題