首页 > 解决方案 > 贷款金额及贷款期限程序

问题描述

编写一个程序,让用户以年数为单位输入贷款金额和贷款期限,并显示从 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 = '')

标签: python

解决方案


我不确定如何计算您需要的值,但据我了解,您需要利率从 3% 开始,每年增加 1/8,即 0.125,并在 5 处停止。如果是这种情况,numPy将很有帮助。你可以interestRate像这样制作一个数组:

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

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


推荐阅读