2016-11-27 68 views
-1

我有一个项目用于创建银行账户类,添加方法并使用存取方法增加/减少账户持有人的余额。下面是代码:在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搜索了,但我没有完全遵循。

+0

''$ 1587.23''不是一个数字。 – user2357112

+1

''帐户余额 - “+ int(self.balance)' - 你认为在那里发生了什么? – user2357112

+0

好的,我改变了,但以零结尾的数字没有正确显示为帐户余额。 –

回答

2

下面是一些提示:

>>> '$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 

确保您使用正确类型的天平,存款,取款和价值观。使用格式化来保留小数点后的数字位数。

参见Format Specification Mini-Language

在acct_info
+0

好的,我修复了那部分。感谢您的帮助。我正在尝试向帐户添加500.00的存款,但我的计划只显示初始金额。我为所有问题表示歉意,但是这是踢我的屁股。 –

0

()尝试将其更改为:

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

感谢您的帮助。我非常感谢它。 –

+0

接受回答然后@ marquis-hinmon-sr! – Malcoolm