尝试为学校做一个简单的银行账户模型,函数 get_balance 会产生错误
Trying to do a simple model of a bank account for school and the function get_balance yields an error
在我 运行 代码通过 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
你的代码有错误。在 get_balance() 函数中,您只返回 self.balance() 应该有 self.balance 的地方。此外,您正在同名测试中创建两个对象 'a1'.
只需将第 9 行替换为:
return self.balance
self.balance
是一个数字,不是函数,因此在您尝试调用它时会产生错误。
更改这些行:
def get_balance(self):
return self.balance #remove '()'
和
a2 = Account(124, 'Donald T.', 1000000) #a2 instead a1
在我 运行 代码通过 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
你的代码有错误。在 get_balance() 函数中,您只返回 self.balance() 应该有 self.balance 的地方。此外,您正在同名测试中创建两个对象 'a1'.
只需将第 9 行替换为:
return self.balance
self.balance
是一个数字,不是函数,因此在您尝试调用它时会产生错误。
更改这些行:
def get_balance(self):
return self.balance #remove '()'
和
a2 = Account(124, 'Donald T.', 1000000) #a2 instead a1