贷款金额和贷款期限方案

时间:2019-02-28 18:20:18

标签: python

编写一个程序,该程序使用户可以输入贷款金额和贷款期限(以年为单位),并显示从3%到5%的每个利率的月度付款和总付款,以1/8为增量。每月付款和总付款的计算公式如下:

我需要1/8增量的帮助。我想到了for循环,但Python不允许使用浮点数。我研究了一下,发现了一个叫做numpy的东西,但是我还没有学到。有办法吗?

这是我到目前为止所拥有的:

monthlyPay = 0
total = 0

#Prompt the user to enter loan amount and loan period in years
loanAmount = float(input("Enter loan amount: $"))
years = int(input("Enter loan period in years: "))                  

#Display Table Header
print("Interest\tMonthly Pay\tTotal")

#Display Table Results
for yr in range(0,years):
    interestRate = 3/(100*12)

    #Calculate Monthly Payment
    monthlyPay = loanAmount*interestRate/(1-(1/((1+interestRate)**(years*12))))

    #Calculate Total Payment
    total = monthlyPay * years * 12
    print(format(interestRate, '.3f'), '%\t\t', format(monthlyPay, '.2f'),\
          '\t\t', format(total, '.2f'), sep = '')

1 个答案:

答案 0 :(得分:0)

我不确定如何计算所需的值,但是据我了解,您需要利率从3%开始,然后每年以1/8(即0.125)递增,然后停止五点钟。在这种情况下,numPy会有所帮助。您可以将interestRate做成这样的数组:

import numpy as np
interestRate = np.arange(3, 3 + 0.125*years, 0.125).clip(max=5)

arange给出了您需要的数组,clip使所有大于5的值都等于5。