【发布时间】:2019-09-21 23:25:49
【问题描述】:
好的,所以我在编写此代码时遇到了问题,并且用于测试的代码是,无论我尝试什么,我似乎都无法让它工作。
有问题的作业说:设计一个名为 Account 的类,其中包含:
■ 一个名为 id 的私有 int 数据字段,用于帐户。
■ 一个名为 balance 的私有浮动数据字段。
■ 一个名为 AnnualInterestRate 的私有浮点数据字段,用于存储当前 利率。
■ 创建具有指定 id(默认为 0)的帐户的构造函数,初始 余额(默认 100)和年利率(默认 0)。
■ id、balance 和annualInterestRate 的访问器和修改器方法。
■ 一个名为 getMonthlyInterestRate() 的方法,它返回每月 利率。
■ 一个名为 getMonthlyInterest() 的方法,用于返回每月利息。
■ 一个名为withdraw 的方法,从 帐户。
■ 一种名为 deposit 的方法,将指定金额存入帐户。
(提示:getMonthlyInterest()方法是返回月利息金额,不是 利率。使用此公式计算每月利息:余额 * 月利率。月利率是年利率 / 12. 请注意,annualInterestRate 是一个百分比(如 4.5%)。你需要 除以 100。)
编写一个测试程序,创建一个帐户 id 为 1122 的 Account 对象,一个 余额为 20,000 美元,年利率为 4.5%。使用提款 方法提取$2,500,使用存款方法存入$3,000,打印 id、余额、月利率、月利息。
这是我目前的课程代码:
class Account:
def __init__(self, accountid = 0, initialbalance = 100, annualInterestrate = 0):
self.accountid = accountid
self.initalbalance = initialbalance
self.annualInterestrate = annualInterestrate
def balance(self):
return float(self.balance)
def id(self):
return int(self.id)
def annualInterestrate(self):
return float(self.annaulInterestrate)
def getid(self):
return self.id
def setbalance(self):
return self.setbalance
def getannualInterestrate(self):
return self.annualInterestrate
def getMonthlyInterestRate(self):
return self.annualInterestrate / 100
def withdraw(self):
amount = 28
if self.balance>=amount:
self.balance-=amount
def deposit(self):
amount = 45
self.balance += amount
def getMonthlyInterest(self):
return balance * monthlyInterestRate. monthlyInterestRate is annualInterestRate / 12
忽略数字 45 和 28,我只是将它们作为占位符放在那里。
这是我为测试程序准备的(目前不完整,因为我迷路了):
from Account import Account
def main():
account1 = Account()
print("Account id # is", account1.getid)
print("Beginning Balance: ", account1.setbalance)
print("Monthly Interest Rate: ", account1.getMonthlyInterestRate())
print("Monthly Interest: ", account1.getMonthlyInterest())
main()
我不知道我在做什么。如果有人知道我应该做什么,请提供帮助。
我试图弄清楚我是如何将信息输入课程(或测试程序,如果需要的话)并让它输出如下内容:
帐号编号为 1122
初始余额:取款和/或存款后的余额
月利率:无论所有数学结果如何。
每月利息:再一次,不管所有的数学结果是什么
【问题讨论】:
-
创建帐户时不应该传递一些信息吗?
Account(id=1122, initialbalance=20000, annualInterestrate=0.045)? -
它通常有助于说明您的预期和实际得到的结果。否则,很难知道“错误”是什么意思。 :-)
-
@JohnSzakmeister 对,抱歉,关于缺少信息,我正试图弄清楚我应该在哪里插入该信息。具体来说,我如何将这些信息带入课堂(或测试程序,如果需要的话)并将其输出如下: 帐户 ID # 是,1122 开始余额:无论余额是什么,然后取款和/或存款 月利率:所有数学结果。 月利率:再次,无论所有数学结果。
标签: python variable-assignment