我正在制作一个程序,它使用类 Account 来打印 accountA 的每月利息金额等。我在解决 getMonthlyInterestRate() 和 getMonthlyInterest 定义时遇到问题。这是迄今为止的程序:
帐户.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 来计算。
除了这两个定义和最后一个打印语句之外,程序中的所有其他内容都是正确的。任何帮助,将不胜感激。谢谢。