我不知道如何更改代码,以便在脚本运行时重置deposit变量,因为如果我首先输入pennies,deposit变量将在每次调用函数时存储该值,因此按下不同的硬币(如nickels),它将不会执行其“elif”语句,而是转到pennies。我该怎么解决这个问题?你知道吗
pennies_total = 0
nickels_total = 0
dimes_toal = 0
quarters_total = 0
keeprunning = True
def deposit():
global pennies_total
global nickels_total
global dimes_total
global quarters_total
deposit = (raw_input("What would you like to deposit? (P for pennies, N for nickels, D for dimes, Q for quarters): ")).upper()
if deposit == 'P' or 'PENNIES':
pennies_instance = raw_input("How many pennies would you like to deposit?: ")
pennies_total = int(pennies_instance) + pennies_total
print "There are %s pennies in your bank"% (pennies_total)
elif deposit == 'N' or 'NICKELS':
nickels_instance = raw_input("How many nickels would you like to deposit?: ")
# create if non-integer is input for all classes of coins
nickels_total = int(nickels_instance) + nickels_total
print "There are %s nickels in your bank"% (nickels_total)
while keeprunning == True:
exc = raw_input("Would you like to deposit or withdraw money? (D for deposit, W for withdraw, Q for Quit): ").upper()
if exc == "D" or "DEPOSIT":
deposit()
两个变化:
1)更改中的变量名:
其他的名字,比如:面额
2)更换:
签署人:
对“all”if和“elif”条件进行此更改。你知道吗
以下表达式:
不是你想的那样。不管
deposit
的值是多少,这个表达式将始终是真的。因此,硬币支路总是被拿走,而镍支路从不被拿走。你知道吗请尝试:
或者
其他的
if
语句也是如此。你知道吗相关问题 更多 >
编程相关推荐