【问题标题】:Class method.. Print the monthly interest amount. Python类方法..打印每月的利息金额。 Python
【发布时间】:2013-04-03 20:38:17
【问题描述】:

我正在制作一个程序,它使用 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 来计算。

除了这两个定义和最后一个打印语句之外,程序中的其他所有内容都是正确的。任何帮助,将不胜感激。谢谢。

【问题讨论】:

  • 打印时不需要直接调用.__str__()print 会为你调用,所以print(accountA) 就足够了。
  • 也不需要用self调用方法。它是自动完成的......
  • 它不是函数...类函数是方法
  • @foriinrangeawesome:其实getBalance 一个函数,只是它不在正确的locals 命名空间中。 (这在 2.x 中并不完全正确,它具有未绑定的方法,但差异并不重要;在 3.x 中确实如此。)因此,如果您执行 Account.getBalance(self)getBalance = self.__class__.getBalance; getBalance(self) 或其他任何操作解决这个问题,它将与self.getBalance() 完全相同。当然,仅仅因为您可以将其作为函数而不是绑定方法调用并不意味着您应该...
  • @Ben:如果你使用方法调用语法,它只会自动完成(所以你会得到一个绑定的方法)。只是getBalance() 是行不通的;你需要self.getBalance()(或getBalance = self.getBalance; getBalance()或类似的东西)。

标签: python class function balance


【解决方案1】:

你应该调用方法on 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

【讨论】:

  • getter 和 setter 是我们程序大纲的一部分。我们只是在学习这个,所以他们想确保我们知道所有的事情。私有所​​需的属性也是如此。但是非常感谢大家的帮助!!!! :) 我只是更改了几件事以使其适用于私有属性。 def getMonthlyInterestRate(self): return self.__annualInterestRate/12 def getMonthlyInterest(self): return self.__balance*self.getMonthlyInterestRate()
【解决方案2】:

你定义

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 2013-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多