python - 尝试为学校做一个简单的银行账户模型,函数 get_balance 会产生错误

标签 python linux python-3.x ubuntu-14.04

在我通过 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/

相关文章:

python - 如何在 Python 中创建一个对象数组?

python - 如何从命令行添加更多鸡蛋?

php - 在 php 中开源文件托管/共享

python:如何将列表中的每个值与另一个列表中的所有值相乘

python - 如何从 Dataframe 列形成元组列表

python - 在 dockerfile 中自定义 ONBUILD 环境

python - 如何根据其他字段提取csv文件某一字段的值?

c - shell 管工艺组 : ls | cat works but ls | more hangs?

linux - 脚本不会在 Linux 中同时执行

python - 类型错误 : Can't convert 'bytes' object to str implicitly despite adding . 编码()到字符串