Level 1: Mortgage Loans - defining abstract classes
Programming requirements
The programming task of this level is to complete the four functions of findPayment, init, makePayment and getTotalPaid in 7-1.py file to meet the requirements of calculating mortgage loans. Specific requirements are as follows:
This pass requires to complete the four functions described above, and help people choose the appropriate way of loan by studying the rules of mortgage loan;
See the following test examples for details.
The code framework of the code file 7-1.py involved in this level is as follows:
def findPayment(loan, r, m):
#Please add code here to complete the function findPayment
#********** Begin #
#* End #
class Mortgage(object):
def init(self, loan, annRate, months):
#Please add code here to complete the function__ init__
#* Begin #
#* End #
self.legend = None
def makePayment(self):
#Please add code here to complete the function makePayment
#* Begin #
#* End #
def getTotalPaid(self):
#Please add code here to complete the function getTotalPaid
#* Begin #
#* End *********#
def str(self):
return 'The Mortgage is {self.legend}, Loan is {self.loan}, Months is {self.months}, Rate is {self.rate:.2f}, Monthly payment is {self.payment:.2f}'.format(self=self)
if name=="main":
print(Mortgage(100000, 6.5, 36))
print(Mortgage(100000, 6.5, 120))
Test description
The platform will test the code you write:
Test input: no input
Expected output:
The Mortgage is None, Loan is 100000, Months is 36, Rate is 0.01, Monthly payment is 3064.90
The Mortgage is None, Loan is 100000, Months is 120, Rate is 0.01, Monthly payment is 1135.48
Start your mission. I wish you success!
code
def findPayment(loan, r, m): #********** Begin *********# # Write the code below return loan*((r*pow(1+r,m)/(pow(1+r,m)-1))) # Please do not modify the following code #********** End *********# class Mortgage(object): def __init__(self, loan, annRate, months): #********** Begin *********# # Write the code below self.loan=loan self.annRate=annRate self.months=months self.rate = self.annRate/12/100 self.paid = [0.0] self.owed = [loan] self.payment = findPayment(loan,self.rate,months) # Please do not modify the following code #********** End *********# self.legend = None def makePayment(self): #********** Begin *********# # Write the code below self.paid.append(self.payment) reduction = self.payment - self.owed[-1] * self.rate self.owed.append(self.owed[-1] - reduction) # Please do not modify the following code #********** End *********# def getTotalPaid(self): #********** Begin *********# # Write the code below return sum(self.paid) # Please do not modify the following code #********** End *********# def __str__(self): return 'The Mortgage is {self.legend}, Loan is {self.loan}, Months is {self.months}, Rate is {self.rate:.2f}, Monthly payment is {self.payment:.2f}'.format(self=self) if __name__=="__main__": print(Mortgage(100000, 6.5, 36)) print(Mortgage(100000, 6.5, 120))
Level 2: modeling of three loan methods
Programming requirements
The programming task of this pass is to complete the code of begin end interval in 7-2.py file and realize the requirements of modeling with three loan methods. Specific requirements are as follows:
It is required to complete the init function and makePayment function defined in 3 subclasses in 7-2.py file to realize the modeling function of three loan modes;
See the following test examples for details.
The code framework of the code file 7-2.py involved in this level is as follows:
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):
#Please add code here to complete the function__ init__
#********** Begin #
#* End #
self.legend = 'Fixed, ' + str® + '%, for ' + str(months) + ' months'
class FixedWithPoints(Mortgage):
def init(self, loan, r, months, pts):
#Please add code here to complete the function__ init__
#* Begin #
#* End #
self.legend = 'Fixed, ' + str® + '%, ' + str(pts) + ' points, for ' + str(months) + ' months'
class TwoRate(Mortgage):
def init(self, loan, r, months, teaserRate, teaserMonths):
#Please add code here to complete the function__ init__
#* Begin #
#* End #
self.legend = str(teaserRate)
+ '% for ' + str(self.teaserMonths)
+ ' months, \n then ' + str® + '%, for ' + str(months) + ' months'
def makePayment(self):
#Please add code here to complete the function makePayment
#* Begin #
#* End *********#
Mortgage.makePayment(self)
if name=="main":
print(Fixed(100000, 6.5, 36))
print(Fixed(100000, 6.5, 120))
print(FixedWithPoints(100000, 6.5, 36, 20))
print(FixedWithPoints(100000, 6.5, 120, 20))
print(TwoRate(100000, 9.0, 36, 4.8, 12))
print(TwoRate(100000, 7.0, 120, 4.8, 36))
Test description
The platform will test the code you write:
Test input: no input
Expected output:
Fixed, 6.5%, for 36 months
Fixed, 6.5%, for 120 months
Fixed, 6.5%, 20 points, for 36 months
Fixed, 6.5%, 20 points, for 120 months
4.8% for 12 months,
then 9.0%, for 36 months
4.8% for 36 months,
then 7.0%, for 120 months
Start your mission. I wish you success!
code
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): # Please add code here to complete the function__ init__ #********** Begin *********# self.loan = loan self.r = r self.months = months #********** End *********# self.legend = 'Fixed, ' + str(r) + '%, for ' + str(months) + ' months' class FixedWithPoints(Mortgage): def __init__(self, loan, r, months, pts): # Please add code here to complete the function__ init__ #********** Begin *********# self.loan = loan self.r = r self.months = months self.pts=pts #********** End *********# self.legend = 'Fixed, ' + str(r) + '%, ' + str(pts) + ' points, for ' + str(months) + ' months' class TwoRate(Mortgage): def __init__(self, loan, r, months, teaserRate, teaserMonths): # Please add code here to complete the function__ init__ #********** Begin *********# self.loan = loan self.r = r self.months = months self.teaserRate=teaserRate self.teaserMonths=teaserMonths #********** End *********# self.legend = str(teaserRate)\ + '% for ' + str(self.teaserMonths)\ + ' months, \n then ' + str(r) + '%, for ' + str(months) + ' months' def makePayment(self): # Please add code here to complete the function makePayment #********** Begin *********# if len(self.paid) == self.teaserMonths + 1: self.rate = self.nextRate self.payment = findPayment(self.owed[-1], self.rate, self.months - self.teaserMonths) #********** End *********# Mortgage.makePayment(self) if __name__=="__main__": print(Fixed(100000, 6.5, 36)) print(Fixed(100000, 6.5, 120)) print(FixedWithPoints(100000, 6.5, 36, 20)) print(FixedWithPoints(100000, 6.5, 120, 20)) print(TwoRate(100000, 9.0, 36, 4.8, 12)) print(TwoRate(100000, 7.0, 120, 4.8, 36))
Level 3: compare the advantages and disadvantages of various loans
Programming requirements
The programming task of this pass is to complete the code of begin end interval in step3/7-3.py file, so as to meet the requirements of comparing the advantages and disadvantages of various loans. Specific requirements are as follows:
It is required to complete the compareMortgages function, then calculate the three loan methods respectively, and finally return the total amount to the bank. On this basis, compare the advantages and disadvantages of the three loan methods;
See the following test examples for details.
The code framework of the code file 7-3.py involved in this level is as follows:
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® + '%, 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® + '%, ' + 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® + '%, 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):
#Please add code here to complete the function compareMortgages
#********** Begin #
#* End #
for m in range(totMonths):
#Please add code here to complete the function 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)
Test description
The platform will test the code you write:
Test input: no input
Expected output:
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
Start your mission. I wish you success!
code
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): # Please add code here to complete the function 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): # Please add code here to complete the function 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)
educoder Linux and Python Programming 2021 answer
https://blog.csdn.net/u014708644/category_11465058.html