我正在为一个类项目编写ATM程序,并且不允许使用全局变量。我在程序中只使用了局部变量,但是它不起作用。
let headers = [
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Bearer " + self.accessToken,
"cache-control": "no-cache"
]
当我运行它时,它只是在我从菜单中输入一个选项而没有任何错误消息后才结束。
答案 0 :(得分:1)
这里没有理由定义函数-只需在if
语句中执行该代码,即可:
def menu(balance):
print("1...Deposit\n2...Withdraw\n3...View Balance")
userChoice = int(input("Please enter your choice now: "))
if userChoice == 1:
deposit = float(input("Please enter the amount you would like to deposit: "))
balance = balance + deposit
elif userChoice == 2:
withdraw = float(input("Please enter the amount you would like to withdraw: "))
balance = balance + withdraw
else:
print("Your balance is", balance)
return balance
...
balance = 0
balance = menu(balance)
什么也没发生的原因是,按照现在的代码方式,您正在定义功能,而不是调用。查看您的缩进-对withdraw()
,deposit()
和balance()
的调用仅在else
块内执行。而且没有任何参数可以引导,如果执行则将导致错误。