2014-05-10 46 views
0

我看過類似的問題,但仍然無法弄清楚這一點。我絕對相信我在某個地方犯了一個非常愚蠢的錯誤,但我似乎無法找到它。混淆爲什麼我得到一個TypeError:'int'對象不可調用

對於此代碼。

class BankAccount: 
    def __init__(self, initial_balance): 
     self.balance = initial_balance 

    def deposit(self, amount): 
     self.deposit = amount 
     self.balance = self.balance + self.deposit 

    def withdraw(self, amount): 
     self.withdraw = amount 
     self.balance = self.balance - self.withdraw 
     self.fee = 5 
     self.total_fees = 0 

     if self.balance < 0: 
      self.balance = self.balance - self.fee 
      self.total_fees += self.fee 

    def get_balance(self): 
     current_balance = self.balance 
     return current_balance 

    def get_fees(self): 
     return self.total_fees 

當我運行代碼的一切工作正常,當我運行這個

my_account = BankAccount(10) 
my_account.withdraw(15) 
my_account.deposit(20) 
print my_account.get_balance(), my_account.get_fees() 

但是,如果我做一個額外的呼叫撤回

my_account = BankAccount(10) 
my_account.withdraw(15) 
my_account.withdraw(15) 
my_account.deposit(20) 
print my_account.get_balance(), my_account.get_fees() 

它拋出這個錯誤。

TypeError: 'int' object is not callable

我不明白爲什麼它能正常工作,直到我再打一個電話才能退出。請幫忙。

回答

4

當你這樣做self.deposit = amount時,你會用金額覆蓋你的deposit方法。 withdrawself.withdraw = amount相同。您需要爲數據屬性賦予與方法不同的名稱(如調用方法withdraw,但屬性withdrawalAmount或類似的東西)。

4

當你這樣做withdraw方法

self.withdraw = amount 

內更換了與任何amount是它。下次您撥打withdraw時,您會收到amount對象。你的情況是int

這同樣適用於deposit

self.deposit = amount 

給你的數據成員的名字是你的方法不同。

相關問題