分类方法..打印月利息金额。蟒蛇

时间:2013-04-03 20:38:17

标签: python class function balance

我正在制作一个程序,使用类帐户打印accountA的每月利息金额等。我遇到了getMonthlyInterestRate()和getMonthlyInterest定义的问题。这是迄今为止的计划:

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)

file 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 个答案:

答案 0 :(得分:5)

您应该在 self上调用方法,而不是通过传递函数:

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

并将其命名为:

print(accountA.getMonthlyInterest()) 

这也适用于getMonthlyInterestRate

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

你使用了很多getter和setter;在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

答案 1 :(得分: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()))
    换句话说,使用所述函数的返回值来调用它们,而不是使用函数本身。在这些功能中,您尝试再次调用它们。由于您没有传递函数,而是传递值,因此“再次调用”失败。

如果你修复了这个bug,你(可能)会让你的程序运行起来,但是你想要一个非常糟糕的程序。

要改善这一点,请按Martijn Pieters's hint

(这个答案应该是一个评论,但这些不能很好地格式化。)