2013-04-03 52 views
0

我正在製作一個程序,該程序使用班級Account來打印accountA的月息金額等等。我在獲取getMonthlyInterestRate()和getMonthlyInterest定義時遇到了問題。下面是該程序迄今:班級方法..打印月度利息額。 Python

Account.py

class Account: 
    def __init__(self,id=0,balance=100.0,annualInterestRate=0.0): 
     self.__id=id 
     self.__balance=balance 
     self.__annualInterestRate=annualInterestRate 
    def getId(self): 
     return self.__id 
    def getBalance(self): 
     return self.__balance 
    def getAnnualInterest(self): 
     return self.__annualInterestRate 
    def setId(self,newid): 
     self.__id=newid 
    def setBalance(self,newbalance): 
     self.__balance=newbalance 
    def setAnnualInterestRate(self,newannualInterestRate): 
     self.__annualInterestRate=newannualInterestRate 
    def getMonthlyInterestRate(self,getAnnualInterest): 
     return(getAnnualInterest(self)/12) 
    def getMonthlyInterest(self,getBalance,getMonthly): 
     return(getBalance(self)*getMonthlyInterestRate(self)) 

    def withdraw(self,amount): 
     if(amount<=self.__balance): 
      self.__balance=self.__balance-amount 
    def deposit(self,amount): 
     self.__balance=self.__balance+amount 
    def __str__(self): 
     return "Account ID : "+str(self.__id)+" Account Balance : "+str(self.__balance)+" Annual Interest Rate : "+str(self.__annualInterestRate) 

下一個

文件test.py

from Account import Account 

def main(): 
    accountA=Account(0,100,0) 
    accountA.setId(1234) 
    accountA.setBalance(20500) 
    accountA.setAnnualInterestRate(0.375) 
    print(accountA.__str__()) 
    accountA.withdraw(500) 
    accountA.deposit(1500) 
    print(accountA.__str__()) 
    print(accountA.getMonthlyInterest(accountA.getBalance(),accountA.getAnnualInterest())) 
main() 

我無法弄清楚如何使getMonthlyInterestRate()和getMonthlyInterest ()的定義,以便能夠推出正確的輸出,即:

Account ID : 1234 Account Balance : 20500 Annual Interest Rate : 0.375 

Account ID : 1234 Account Balance : 21500 Annual Interest Rate : 0.375 

Monthly Interest Amount : 671.875 

雷永遠是與錯誤的語句:

Account ID : 1234 Account Balance : 20500 Annual Interest Rate : 0.375 
Account ID : 1234 Account Balance : 21500 Annual Interest Rate : 0.375 
Traceback (most recent call last): 
    File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 13, in <module> 
    File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 12, in main 
    File "C:\Users\Meagan\Documents\University\2nd Year\Cmput 174\Account.py", line 21, in getMonthlyInterest 
    return(getBalance(self)*getMonthlyInterestRate(self)) 
builtins.TypeError: 'int' object is not callable 

這是我應該做:

  • 返回月息)命名getMonthlyInterestRate(的方法。

  • 一個名爲getMonthlyInterest()的方法,返回每月的利息金額。每月利息金額可以使用餘額*月利率計算。每月利息可以通過將年利率由12

一切都在計劃別的是,除了這兩個定義,最後print語句正確計算。任何幫助,將不勝感激。謝謝。

+2

你並不需要調用'.__ STR __()'直接打印時; 'print'會爲你打電話,所以'print(accountA)'就足夠了。 – 2013-04-03 20:39:39

+0

也不需要用'self'調用方法。它是自動完成的... – Ben 2013-04-03 20:40:45

+0

它不是一個函數...類函數是一個方法 – erdekhayser 2013-04-03 20:44:16

回答

5

你應該叫方法self,不受周圍路過的功能:

def getMonthlyInterest(self): 
    return self.getBalance() * self.getMonthlyInterestRate() 

,並稱之爲:

print(accountA.getMonthlyInterest()) 

這同樣適用於getMonthlyInterestRate還有:

def getMonthlyInterestRate(self): 
    return self.getAnnualInterest()/12 

你用了很多o f吸氣劑和吸附劑; Python中不需要這些;你不需要讓私有屬性,只需訪問它們,而不是直接:

class Account: 
    def __init__(self, id=0, balance=100.0, annualInterestRate=0.0): 
     self.id = id 
     self.balance = balance 
     self.annualInterestRate = annualInterestRate 

    def getMonthlyInterestRate(self): 
     return self.annualInterestRate/12 

    def getMonthlyInterest(self): 
     return self.balance * self.getMonthlyInterestRate() 

    def withdraw(self, amount): 
     if amount <= self.balance: 
      self.balance -= amount 

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

    def __str__(self): 
     return "Account ID : {0.id} Account Ballance : {0.balance} Annual Interest Rate : {0.annualInterestRate}".format(self) 

然後運行:

def main(): 
    accountA = Account(0,100,0) 
    accountA.id = 1234 
    accountA.balance = 20500 
    accountA.annualInterestRate = 0.375 
    print(accountA) 
    accountA.withdraw(500) 
    accountA.deposit(1500) 
    print(accountA) 
    print(accountA.getMonthlyInterest()) 

結果:

Account ID : 1234 Account Ballance : 20500 Annual Interest Rate : 0.375 
Account ID : 1234 Account Ballance : 21500 Annual Interest Rate : 0.375 
671.875 
+0

作爲我們綱要的一部分,需要吸氣劑和吸附劑。我們正在學習這一點,所以他們想確保我們知道所有的事情。私人所需的屬性也是如此。但是,謝謝你讓所有人都支持你! :)我只是改變了一些東西,使其爲私人屬性工作。 'def getMonthlyInterestRate(self): return self。__annualInterestRate/12 def getMonthlyInterest(self): return self .__ balance * self.getMonthlyInterestRate()' – 2013-04-03 21:00:29

0

您定義

def getMonthlyInterestRate(self,getAnnualInterest): 
    return(getAnnualInterest(self)/12) 
def getMonthlyInterest(self,getBalance,getMonthly): 
    return(getBalance(self)*getMonthlyInterestRate(self)) 

並將它們用作

print(accountA.getMonthlyInterest(accountA.getBalance(),accountA.getAnnualInterest())) 
  • 換句話說,你給他們打電話與上述函數的返回值,而不是用函數本身。在這些功能中,您嘗試再次調用它們。由於你沒有傳遞函數,但是值,這個「再次調用」失敗。

如果你修復這個錯誤,你(可能)使你的程序工作,但你得到一個非常糟糕的風格寫的程序。

要改善這一點,請按照Martijn Pieters's hint

(這個答案也許應該是一個評論,但這些都不能很好地被格式化。)