Python 中的利息计算器。计算总金额和每年支付的金额

Interest calculator in Python. Find total sum and how much to pay each year

我的目标是求出五年后的贷款总额,以及每年要还多少。

到目前为止我的代码:

years = 5

loan = 50000

interest = 0.05


for year in range(years):
    loan += loan * interest
    print(loan)

这是查找每年付款的正确方法吗?

sum = loan / years + loan * interest

如果您不想自己计算,可以使用 numpy_financial

(注意:现在建议使用 numpy-financial,因为 numpy 本身的财务功能已被弃用。

这将通过 pip install numpy-financial 使用 pip 安装。

>>> import numpy_financial as npf
>>> npf.pmt(.05,5,-50000)
11548.739906413395

以上给出的是年付。

因此,第一年的本金和利息支付金额为:

利息 = 50000*.05

principal_paid = 11548.74 - 利息

这是一个执行该操作的小程序。

import numpy_financial as npf

rate = .05
principal = 50000
years = 5

annual_pay = npf.pmt(rate,years,-principal)

print('{}{:>10}{:>10}{:>10}'.format('year','interest','retired', 'balance'))

for yr in range(1,6):
    interest_to_pay = rate * principal
    retired_prin = annual_pay - interest_to_pay
    principal = principal - retired_prin
    print('{:>4}{:>10.2f}{:>10.2f}{:>10.2f}'
          .format(yr, interest_to_pay, retired_prin, principal))
    

这会打印:

year  interest   retired   balance
   1   2500.00   9048.74  40951.26
   2   2047.56   9501.18  31450.08
   3   1572.50   9976.24  21473.85
   4   1073.69  10475.05  10998.80
   5    549.94  10998.80      0.00