【问题标题】:Create an Account class in Python 3在 Python 3 中创建一个 Account 类
【发布时间】:2013-11-01 04:35:56
【问题描述】:

我想知道是否有人可以帮助我解决这个问题?我正在使用 python 3,我正在寻找这样做。不过,我已经无可救药地迷失了。任何帮助将不胜感激!

Account 类应包含以下内容 账户的一个名为 id 的私有 int 数据字段,默认值为 0。

账户的一个名为 balance 的私有双数据字段,默认值为 0。

一个名为 AnnualInterestRate 的私有双数据字段,存储当前利率,默认值为 0。假设所有账户的利率相同。

一个名为 dateCreated 的私有日期数据字段,用于存储帐户的创建日期。

创建默认帐户的无参数构造函数。

创建具有指定 ID 和初始余额的帐户的构造函数。

id、balance和annualInterestRate的访问器和修改器方法。

dateCreated 的访问器方法。

一个名为 getMonthlyInterestRate() 的方法,它返回月利率。

一个名为withdraw的方法,从账户中提取指定的金额。

一种名为 deposit 的方法,将指定的金额存入帐户。

实现类。编写一个测试程序,创建一个 Account 对象,账户 ID 为 1122,余额为 20,000 美元,年利率为 4.5%。使用withdraw 方式提取$2,500,使用deposit 方式存入$3,000,并打印余额、每月利息以及该账户的创建日期。

我现在的代码是:

class Account:

    def __init__(self):
        self.balance=balance

    def getMonthlyInterestRate(self):
        return (annualInterest/100)

    def getMonthlyInterest(self):
        return blanace * monthlyInterestRate

现在我的另一个代码是:

from Account import Account

def main():

    account = Account()
    account.getMonthlyInterestRate(4.5)

    print("Beginning Balance: ", account.balance)
    print("Monthly Interest Rate: ", account.getMonthlyInterestRate)
    print("Monthly Interest: ", account.getMonthlyInterest)

main()

【问题讨论】:

  • 问题是什么?
  • 如何设置?当我现在运行代码时,我收到一条错误消息,“未定义全局名称'balance'”。我对创建 1122 的帐户 ID 感到特别困惑。
  • 不,这不是家庭作业。我只是想学习python。它来自购买的书。
  • 这个任务听起来像是为使用 Python 以外的语言编程的人准备的。提到了一堆东西,它们要么在 Python 中根本不存在(例如私有的类型化变量),要么在 Python 中的工作方式与在其他语言中的工作方式不同(具有不同数量参数的多个构造函数) .如果您正在阅读 Java 或 C++ 书籍,那么学习 Python 将非常困难!

标签: python class python-3.x


【解决方案1】:

只需将你的类修改为这样:

 class Account:

    def __init__(self, balance, annual_interest_rate):
        self.balance=balance
        self.annualInterest = annual_interest_rate

    def getMonthlyInterestRate(self):
        return (self.annualInterest/100)

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

请注意,我已将余额和年利率作为构造函数参数传递 所以要实例化一个帐户对象:

account = Account(200, 5)

【讨论】:

  • 我不断得到一个 显示。
  • 需要加上圆括号 --> print("Monthly Interest Rate: ", account.getMonthlyInterestRate()) print("Monthly Interest: ", account.getMonthlyInterest())
【解决方案2】:

让我们看看你的代码-

class Account:

    def __init__(self):
        self.balance=balance

这是你的构造函数。它设置您的类实例的默认状态 - 当您构建帐户时,此方法将运行。在此方法中, self 指的是您正在设置的特定帐户。如果你打电话

myaccount = Account()

myaccount.balance 将等于我们设置的任何值。这是我们解决第一个问题的地方——你设置 self.balance = balance.... 但是 balance 不等于任何东西!但是你可以有

def __init__(self):
    self.balance = 0
    self.id = 0
    self.annualInterestRate = 0

等等,用它们的默认值设置这些变量。

让我们看看你的下一个方法-

def getMonthlyInterestRate(self):
    return (annualInterest/100)

在这里,您设置了一个方法,当您调用 myaccount.getMonthlyInterestRate() 时应该返回利率。但是,您没有指定要从中提取的变量。那应该是 返回 self.annualInterestRate/100 因为你想要这个账户的利息。

现在你的最后一个类方法-

def getMonthlyInterest(self):
    return blanace * monthlyInterestRate

这有两个问题。首先,它应该是 self.balance- 因为这是你想要的这个账户的余额。其次,monthlyInterestRate 不是一个变量——它是一个派生值,你有一个函数。你可以调用那个函数...

return balance * getMonthlyInterestRate()

看看你的主要功能-

account.getMonthlyInterestRate(4.5)

您将该利率输入函数...但该函数不接受任何参数!如果你想设置你会使用的费率

account.annualInterestRate = 4.5

或创建一个 SET 利率函数,该函数接受一个参数,然后将变量设置为适当的值。这将是

def setInterest(self, rate):
    self.annualInterestRate = rate

同样的逻辑将适用于您的其余要求...祝 Python 好运 :)

【讨论】:

    猜你喜欢
    • 2021-06-13
    • 1970-01-01
    • 2021-01-16
    • 2018-02-05
    • 2016-02-29
    • 1970-01-01
    • 2017-09-26
    • 2021-08-29
    • 2021-07-26
    相关资源
    最近更新 更多