我是Python的新手,我正在为一个银行账户编写一些代码,比如说,我在创建适当的代码来显示账户发生变化后的整体余额时遇到了麻烦。下面是我的代码;
account = 1000.
balance = 0
def deposit ( x, y):
return x + account
def withdrawal (x, y):
return account - x
def getbalance (account,depost,withdrawal):
return account + deposit - withdrawal
print("1. Make a Deposit")
print("2. Make a Withdrawal")
print("3. Obtain Balance")
print("4. Quit")
choice = input("Make a selection from the option menu ")
if choice == '1':
num1 = int(input("Enter amount of Deposit: "))
print("Enter Amount of Deposit. ", deposit(num1,1000))
elif choice == '2':
num2 = int(input("Enter amount of Withdrawal: "))
print(num2, "Withdrawal Processed. ", withdrawal(num2,1000))
elif choice == '3':
balance=getbalance(account,deposit,withdrawal)
print(balance, "Balance: ", balance(1000,deposit,withdrawal))并且我一直得到下面的typeError:
line 23, in getbalance
return account + deposit - withdrawal
TypeError: unsupported operand type(s) for +: 'float' and 'function任何人在这个问题上都有任何建议,我都很乐意。感谢大家抽出宝贵的时间。
发布于 2017-02-03 11:59:48
存款不是一个变量,它是一个函数
您应该只更新每个函数中的余额,而不是将其返回出去
例如,你可以这样做
balance = 1000.
while true
if choice == '1':
deposit = float(input("Enter amount of Deposit: "));
balance += deposit
print("Deposit of {} processed, new balance {}".format(deposit,balance))
if choice == '2':
...你也不需要。,并考虑将变量命名为更有意义的名称。
发布于 2017-02-03 11:43:16
是个打字错误。
您的参数是depost,但是您在返回语句中使用了deposit。deposit恰好也是一个函数的名称,所以这就是我们所使用的。
https://stackoverflow.com/questions/42016304
复制相似问题