首页 > 解决方案 > 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

无限循环

标签: python

解决方案


问题是您正在从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 :
     ...

推荐阅读