Linux 与 Python编程2021 Python面向对象编程实训 educoder实训

第1关:按揭贷款——定义抽象类

编程要求

本关的编程任务是补全 7-1.py 文件中 findPayment 、init 、 makePayment 以及 getTotalPaid 四个函数,以实现计算按揭贷款的要求。具体要求如下:

本关要求补全上述描述的 4 个函数,并通过研究按揭贷款的规则,来帮助人们选择合适的贷款方式;
具体请参见后续测试样例。
本关涉及的代码文件7-1.py的代码框架如下:

def findPayment(loan, r, m):
# 请在此添加代码,补全函数findPayment
#********** Begin #
#
* End #
class Mortgage(object):
def init(self, loan, annRate, months):
# 请在此添加代码,补全函数__init__
#
* Begin #
#
* End #
self.legend = None
def makePayment(self):
# 请在此添加代码,补全函数makePayment
#
* Begin #
#
* End #
def getTotalPaid(self):
# 请在此添加代码,补全函数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))

测试说明

平台会对你编写的代码进行测试:

测试输入:无输入
预期输出:

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
开始你的任务吧,祝你成功!

代码

def findPayment(loan, r, m):
	#********** Begin *********#
    # 请在下面编写代码
    return loan*((r*pow(1+r,m)/(pow(1+r,m)-1)))
    # 请不要修改下面的代码
	#********** End *********#
class Mortgage(object):
    def __init__(self, loan, annRate, months):
    	#********** Begin *********#
        # 请在下面编写代码
        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)
        # 请不要修改下面的代码
        #********** End *********#
        self.legend = None

    def makePayment(self):
    	#********** Begin *********#
        # 请在下面编写代码
        self.paid.append(self.payment)
        reduction = self.payment - self.owed[-1] * self.rate
        self.owed.append(self.owed[-1] - reduction)
        # 请不要修改下面的代码
        #********** End *********#

    def getTotalPaid(self):
    	#********** Begin *********#
        # 请在下面编写代码
        return sum(self.paid)
        # 请不要修改下面的代码
        #********** 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))


第2关:三种贷款方式建模

编程要求

本关的编程任务是,补全 7-2.py文件中 Begin-End 区间的代码,实现用三种贷款方式建模的要求。具体要求如下:

本关要求补全7-2.py文件中,3 个子类定义的 init 函数和 makePayment 函数,以实现三种贷款方式建模的功能;
具体请参见后续测试样例。
本关涉及的代码文件 7-2.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):
# 请在此添加代码,补全函数__init__
#********** Begin #
#
* End #
self.legend = 'Fixed, ’ + str® + ‘%, for ’ + str(months) + ’ months’
class FixedWithPoints(Mortgage):
def init(self, loan, r, months, pts):
# 请在此添加代码,补全函数__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):
# 请在此添加代码,补全函数__init__
#
* Begin #
#
* End #
self.legend = str(teaserRate)
+ '% for ’ + str(self.teaserMonths)
+ ’ months, \n then ’ + str® + ‘%, for ’ + str(months) + ’ months’
def makePayment(self):
# 请在此添加代码,补全函数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))

测试说明

平台会对你编写的代码进行测试:

测试输入:无输入
预期输出:

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
开始你的任务吧,祝你成功!

代码

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):
        # 请在此添加代码,补全函数__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):
        # 请在此添加代码,补全函数__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):
        # 请在此添加代码,补全函数__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):
        # 请在此添加代码,补全函数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))

第3关:比较各种贷款的利弊

编程要求

本关的编程任务是,补全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® + ‘%, 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):
# 请在此添加代码,补全函数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)

educoder Linux与Python编程2021 答案
https://blog.csdn.net/u014708644/category_11465058.html

Guess you like

Origin blog.csdn.net/u014708644/article/details/121254077