需要了解在python中创建银行帐户时的错误

需要了解在python中创建银行帐户时的错误,python,python-3.5,Python,Python 3.5,我有一个项目要创建一个银行账户类,添加方法,并使用存款和取款方法增加/减少账户持有人的余额。代码如下: 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

我有一个项目要创建一个银行账户类,添加方法,并使用存款和取款方法增加/减少账户持有人的余额。代码如下:

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追加到当前余额

任何帮助都将不胜感激。如果有任何批评,我理解。我已经在谷歌上搜索过了,但我并没有完全做到这一点。

这里有一些提示:

>>> '$300.10' + 500  # adding a string to an int
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Can't convert 'int' object to str implicitly

>>> 300.10 + 500  # adding a float to an int
800.1

>>> '$300.10' + str(500)   # When using strings
'$300.10500'

>>> print(300.10)        # loss of zero
300.1

>>> print('${:.2f}'.format(300.10))  # formatting
$300.10
>'$300.10'+500#向整数添加字符串
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:无法将“int”对象隐式转换为str
>>>300.10+500#将浮点添加到整数
800.1
>>>使用字符串时“$300.10”+str(500)#
'$300.10500'
>>>打印(300.10)#零损耗
300.1
>>>打印(“${.2f}.”格式(300.10))#格式
$300.10
确保余额、存款和取款值的类型正确。使用格式设置保留超过小数点的位数

请参阅帐户信息()中的。

,尝试将其更改为:

def acct_info(self):
  print("Account Name - "+self.acct_name + ":"+" Account Balance - "+  str(self.balance) +":" +" Account Number - "+self.acct_num + ".")

“$1587.23”
不是一个数字。好的,我更改了它,但是以零结尾的数字不能正确地显示为帐户余额。
“帐户余额-”+int(self.Balance)
-你认为那里发生了什么?我希望它显示与用户帐户余额相关的数字,但我的代码中的错误使它试图将其转换为字符串。我不知道我在哪里犯了那个错误。错误消息说“不能隐式地将'int'对象转换为str”。当您尝试添加一个字符串和一个整数时,您会得到一个错误,因为Python不会隐式地将int转换为字符串进行连接。你必须显式地执行转换。好的,我把那部分修好了。谢谢你的帮助。我正在尝试向一个帐户添加500.00的存款,但我的程序只显示初始金额。我为所有的问题道歉,但这是踢我的屁股。