[英]adding keys and values to dictionaries
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
變量transactions
是main
局部變量,這意味着只有main
內部的代碼才能訪問它,可以通過在函數外部定義它來全局化它,也可以將其作為參數傳遞給需要它的函數。
您是否嘗試過將可變事務傳遞給函數?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.