在我通过 python 3 运行代码后,我得到了这个错误
TypeError: 'int' object is not callable
这是我的代码...
class Account:
def __init__(self, number, name, balance):
self.number = number
self.name = name
self.balance = int(balance)
def get_balance(self):
return self.balance()
def deposit(self, amount):
self.balance += amount
return self.balance
def withdraw(self, amount):
self.balance -= amount
return self.balance
def test():
print('Accounts:')
a1 = Account(123, 'Scott', 10)
a1 = Account(124, 'Donald T.', 1000000)
print('Testing get_balance:')
print(a1.get_balance())
print(a2.get_balance())
print('Testing deposit:')
a1.deposit(100)
a2.deposit(50)
print(a1.get_balance())
print(a2.get_balance())
print('Testing withdraw:')
a1.withdraw(100)
a2.withdraw(50)
print(a1.get_balance())
print(a2.get_balance())
我运行 python 并输入这个...
>>>import bank
>>>bank.test()
后记,这是打印出来的
Accounts:
Testing get_balance:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/scottb0898/CSE1010/HW5/bank.py", line 23, in test
print(a1.get_balance())
File "/home/scottb0898/CSE1010/HW5/bank.py", line 9, in get_balance
return self.balance()
TypeError: 'int' object is not callable
>>>
我确信这是一个非常简单的修复,但我就是无法弄清楚哪里出了问题。 感谢您的帮助!
最佳答案
self.balance
只是一个变量,不是函数。不需要调用,将get_balance
改为
def get_balance(self):
return self.balance
关于python - 尝试为学校做一个简单的银行账户模型,函数 get_balance 会产生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43700172/