Python中的利息计算器。找出总金额和每年要支付的金额 [英] Interest calculator in Python. Find total sum and how much to pay each year

查看:0
本文介绍了Python中的利息计算器。找出总金额和每年要支付的金额的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是找出五年后贷款的总额,以及每年应该支付多少。

我到目前为止的代码:

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

本金_已支付=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

这篇关于Python中的利息计算器。找出总金额和每年要支付的金额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆