python - TidBit 电脑商店的信用计划
问题描述
以购买价格为输入的程序。该程序应该显示一个带有适当标题的表格,其中包含贷款生命周期的付款时间表。
我是 python 编码的新手,我已经尝试了几个小时,我发布的结果是我能做的最好的。我曾尝试使用“休息”,但这个结果只让我在前两个月。
while ending> 0:
Month+=1
payment = starting N * .05
ending-=payment
interest = (starting N * rate) / 12
principal = payment - interest
starting N = ending + payment
无限循环
解决方案
问题是您正在从ending
一个值payment
(
定影:
ending = 1000
N = ending
作为起始值,您将获得:
while ending> 0:
Month+=1
payment = N * .05
ending-=payment
interest = (starting N * rate) / 12 # don't think about this value now
principal = payment - interest # don't think about this value now
N = ending + payment # N becomes the previous ending, without subtracting payment
print("Ending: ", ending, "Payment:", payment)
# Ending: 4e-323 Payment: 0.0
# Ending: 4e-323 Payment: 0.0
# Ending: 4e-323 Payment: 0.0
...
基本上,您最后支付的金额变得如此之小,以至于值不会低于4e-323
,这实际上接近于 0。
因为4e-323 * 0.05
在 python 中基本上是 0,所以你得到一个永远接近 0 而不会变小的值。
payment = 4e-323 * 0.05 # 0
ending -= payment # 4e-323 - 0 = 4e-323
一种解决方案是在 while 语句中设置条件,如下所示:
while ending > 0.00000001 :
...
推荐阅读
- c# - 如何在 C# 中创建包含不同类型的字典类型?
- java - 如何在Java中等待函数内部的函数调用,这可能吗?
- javascript - 如何改进功能中的标志?
- google-cloud-firestore - 如何对数据库中的云 Firestore 文档进行排序?
- opencv - Yolo Darknet:如何标记图像
- android - 使用静态处理程序更新 UI 线程
- c++ - 在 C++ 中输出某些字符串
- go - 安装wirelatency时没有这样的文件或目录
- git - 从多模块 Maven 项目中删除和忽略目标文件夹
- python - Django TabularInline 通过关系