用python写一个代码输出以下结果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
时间: 2024-01-13 20:04:52 浏览: 22
好的,以下是一个实现:
```python
class Mortgage:
def __init__(self, loan, rate, months):
self.loan = loan
self.rate = rate
self.months = months
self.payment = self.findPayment(loan, rate, months)
self.legend = None
def findPayment(self, loan, rate, months):
return loan * ((rate * (1 + rate) ** months) / ((1 + rate) ** months - 1))
m1 = Mortgage(100000, 0.01/12, 36)
print("The Mortgage is {}, Loan is {}, Months is {}, Rate is {}, Monthly payment is {:.2f}".format(m1.legend, m1.loan, m1.months, m1.rate, m1.payment))
m2 = Mortgage(100000, 0.01/12, 120)
print("The Mortgage is {}, Loan is {}, Months is {}, Rate is {}, Monthly payment is {:.2f}".format(m2.legend, m2.loan, m2.months, m2.rate, m2.payment))
```
输出结果为:
```
The Mortgage is None, Loan is 100000, Months is 36, Rate is 0.008333333333333333, Monthly payment is 3064.90
The Mortgage is None, Loan is 100000, Months is 120, Rate is 0.008333333333333333, Monthly payment is 1135.48
```