我正在製作一個程序,該程序使用班級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語句正確計算。任何幫助,將不勝感激。謝謝。
你並不需要調用'.__ STR __()'直接打印時; 'print'會爲你打電話,所以'print(accountA)'就足夠了。 – 2013-04-03 20:39:39
也不需要用'self'調用方法。它是自動完成的... – Ben 2013-04-03 20:40:45
它不是一個函數...類函數是一個方法 – erdekhayser 2013-04-03 20:44:16