任务描述
本关利用上一关实现的三个类,比较三种贷款方式,在同等额度贷款、同等时限下,最后还给银行的总金额。
相关知识
请补全 compareMortgages 函数,compareMortgages 函数的各参数说明如下:
amt:贷款总额;
years:贷款年限;
fixedRate:固定利息率,不预付点数贷款方式的利息率;
pts:预付点数;
ptsRate:固定利息率,但预付一定点数贷款方式的利息率;
varRate1:可变利息贷款的引诱利率;
varRate2:可变利息贷款的第二种利息率;
varMonths:引诱利息月份。
编程要求
本关的编程任务是,补全step3/7-3.py文件中 Begin-End 区间的代码,以实现比较各种贷款利弊的要求。具体要求如下:
本关要求补全 compareMortgages 函数,然后分别计算三种贷款方式,最后还给银行的总金额,以此为依据,来对比三种贷款方式的利弊;
具体请参见后续测试样例。
本关涉及的代码文件7-3.py的代码框架如下:
def findPayment(loan, r, m):
return loan * ((r * (1 + r) ** m) / ((1 + r) ** m - 1))
class Mortgage(object):
def __init__(self, loan, annRate, months):
self.loan = loan
self.rate = annRate / 1200.0
self.months = months
self.paid = [0.0]
self.owed = [loan]
self.payment = findPayment(loan, self.rate, self.months)
self.legend = None
def makePayment(self):
self.paid.append(self.payment)
reduction = self.payment - self.owed[-1] * self.rate
self.owed.append(self.owed[-1] - reduction)
def getTotalPaid(self):
return sum(self.paid)
def __str__(self):
return str(self.legend)
class Fixed(Mortgage):
def __init__(self, loan, r, months):
Mortgage.__init__(self, loan, r, months)
self.legend = 'Fixed, ' + str(r) + '%, for ' + str(months) + ' months'
class FixedWithPoints(Mortgage):
def __init__(self, loan, r, months, pts):
Mortgage.__init__(self, loan, r, months)
self.pts = pts
self.paid = [loan * (pts / 100.0)]
self.legend = 'Fixed, ' + str(r) + '%, ' + str(pts) + ' points, for ' + str(months) + ' months'
class TwoRate(Mortgage):
def __init__(self, loan, r, months, teaserRate, teaserMonths):
Mortgage.__init__(self, loan, teaserRate, months)
self.teaserMonths = teaserMonths
self.teaserRate = teaserRate/1200
self.nextRate = r / 1200.0
self.legend = str(teaserRate)\
+ '% for ' + str(self.teaserMonths)\
+ ' months, \n then ' + str(r) + '%, for ' + str(months) + ' months'
def makePayment(self):
if len(self.paid) == self.teaserMonths + 1:
self.rate = self.nextRate
self.payment = findPayment(self.owed[-1], self.rate, self.months - self.teaserMonths)
Mortgage.makePayment(self)
def compareMortgages(amt, years, fixedRate, pts, ptsRate, varRate1, varRate2, varMonths):
# 请在此添加代码,补全函数compareMortgages
#********** Begin *********#
#********** End *********#
for m in range(totMonths):
# 请在此添加代码,补全函数compareMortgages
#********** Begin *********#
#********** End *********#
for m in morts:
print(m)
print('Loan ' + str(amt) + ' Total payments = ' + str(int(m.getTotalPaid())))
if __name__=="__main__":
compareMortgages(200000, 30, 7, 3.25, 5, 4.5, 9.5, 48)
print('*'*40)
compareMortgages(1000000, 30, 7, 20, 5, 4.5, 9.5, 48)
print('*' * 40)
compareMortgages(500000, 10, 7, 20, 5, 4.5, 9.5, 48)
测试说明
平台会对你编写的代码进行测试:
测试输入:
无输入
预期输出:
Fixed, 7%, for 360 months
Loan 200000 Total payments = 479017
Fixed, 5%, 3.25 points, for 360 months
Loan 200000 Total payments = 393011
4.5% for 48 months,
then 9.5%, for 360 months
Loan 200000 Total payments = 551444
Fixed, 7%, for 360 months
Loan 1000000 Total payments = 2395088
Fixed, 5%, 20 points, for 360 months
Loan 1000000 Total payments = 2132557
4.5% for 48 months,
then 9.5%, for 360 months
Loan 1000000 Total payments = 2757224
Fixed, 7%, for 120 months
Loan 500000 Total payments = 696650
Fixed, 5%, 20 points, for 120 months
Loan 500000 Total payments = 736393
4.5% for 48 months,
then 9.5%, for 120 months
Loan 500000 Total payments = 678254
开始你的任务吧,祝你成功!
只要路是对的,就不怕路远。
代码示例
def findPayment(loan, r, m):
return loan * ((r * (1 + r) ** m) / ((1 + r) ** m - 1))
class Mortgage(object):
def __init__(self, loan, annRate, months):
self.loan = loan
self.rate = annRate / 1200.0
self.months = months
self.paid = [0.0]
self.owed = [loan]
self.payment = findPayment(loan, self.rate, self.months)
self.legend = None
def makePayment(self):
self.paid.append(self.payment)
reduction = self.payment - self.owed[-1] * self.rate
self.owed.append(self.owed[-1] - reduction)
def getTotalPaid(self):
return sum(self.paid)
def __str__(self):
return str(self.legend)
class Fixed(Mortgage):
def __init__(self, loan, r, months):
Mortgage.__init__(self, loan, r, months)
self.legend = 'Fixed, ' + str(r) + '%, for ' + str(months) + ' months'
class FixedWithPoints(Mortgage):
def __init__(self, loan, r, months, pts):
Mortgage.__init__(self, loan, r, months)
self.pts = pts
self.paid = [loan * (pts / 100.0)]
self.legend = 'Fixed, ' + str(r) + '%, ' + str(pts) + ' points, for ' + str(months) + ' months'
class TwoRate(Mortgage):
def __init__(self, loan, r, months, teaserRate, teaserMonths):
Mortgage.__init__(self, loan, teaserRate, months)
self.teaserMonths = teaserMonths
self.teaserRate = teaserRate/1200
self.nextRate = r / 1200.0
self.legend = str(teaserRate)\
+ '% for ' + str(self.teaserMonths)\
+ ' months, \n then ' + str(r) + '%, for ' + str(months) + ' months'
def makePayment(self):
if len(self.paid) == self.teaserMonths + 1:
self.rate = self.nextRate
self.payment = findPayment(self.owed[-1], self.rate, self.months - self.teaserMonths)
Mortgage.makePayment(self)
def compareMortgages(amt, years, fixedRate, pts, ptsRate, varRate1, varRate2, varMonths):
# 请在此添加代码,补全函数compareMortgages
#********** Begin *********#
totMonths = years * 12
fixed1 = Fixed(amt, fixedRate, totMonths)
fixed2 = FixedWithPoints(amt, ptsRate, totMonths, pts)
twoRate = TwoRate(amt, varRate2, totMonths, varRate1, varMonths)
morts = [fixed1, fixed2, twoRate]
#********** End *********#
for m in range(totMonths):
# 请在此添加代码,补全函数compareMortgages
#********** Begin *********#
for mort in morts:
mort.makePayment()
#********** End *********#
for m in morts:
print(m)
print('Loan ' + str(amt) + ' Total payments = ' + str(int(m.getTotalPaid())))
if __name__=="__main__":
compareMortgages(200000, 30, 7, 3.25, 5, 4.5, 9.5, 48)
print('*'*40)
compareMortgages(1000000, 30, 7, 20, 5, 4.5, 9.5, 48)
print('*' * 40)
compareMortgages(500000, 10, 7, 20, 5, 4.5, 9.5, 48)