我有一个项目用于创建银行账户类,添加方法并使用存取方法增加/减少账户持有人的余额。下面是代码:在python中创建银行账户时需要了解我的错误
class BankAccount():
interest = 0.01
def __init__(self, acct_name, acct_num, balance):
self.acct_num = acct_num
self.acct_name = acct_name
self.balance = balance
def deposit(self, amount):
"""Make a deposit into the account."""
self.balance = self.balance + int(amount)
def withdrawal(self, amount):
"""Make a withdrawal from the account."""
self.balance = self.balance - amount
def add_interest(self, interest):
"""Add interest to the account holder's account."""
self.balance = self.balance * interest
def acct_info(self):
print("Account Name - " + self.acct_name + ":" + " Account Balance - " + int(self.balance) + ":" + " Account Number - " + self.acct_num + ".")
acct1 = BankAccount('Moses Dog', '554874D', 126.90)
acct1.deposit(500)
acct1.acct_info()
print(" ")
acct2 = BankAccount('Athena Cat', '554573D', '$1587.23')
acct2.acct_info()
print(" ")
acct3 = BankAccount('Nick Rat', '538374D', '$15.23')
acct3.acct_info()
print(" ")
acct4 = BankAccount('Cassie Cow', '541267D', '$785.23')
acct4.acct_info()
print(" ")
acct5 = BankAccount('Sam Seagull', '874401D', '$6.90')
acct5.acct_info()
print(" ")
当我打电话acct1.deposit(500)的方法,我得到“int对象不能转换为字符串含蓄”。
如果我将int(amount)更改为str(amount)并运行它,它会将500添加到当前余额中。
任何帮助,将不胜感激。我明白是否有任何批评。我用Google搜索了,但我没有完全遵循。
''$ 1587.23''不是一个数字。 – user2357112
''帐户余额 - “+ int(self.balance)' - 你认为在那里发生了什么? – user2357112
好的,我改变了,但以零结尾的数字没有正确显示为帐户余额。 –