Interest calculator in Python. Find total sum and how much to pay each year(Python中的利息计算器。找出总金额和每年要支付的金额)
本文介绍了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中的利息计算器。找出总金额和每年要支付的金额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:Python中的利息计算器。找出总金额和每年要支付的金额


基础教程推荐
猜你喜欢
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 包装空间模型 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01