1 回答
TA贡献1816条经验 获得超4个赞
我认为这就是您要实现的目标。切换到 while 循环更合适,因为您不确定需要循环多少次。只要 x 大于 0,这种情况就会继续。
循环方法
x = (SA + (AI / 12) * SA) - MP
payment_number = 0
while x > 0:
x = (x + (AI / 12) * x) - MP
CIwoP = (x + (AI / 12) * x) - x # interest every month
ptd = MP * payment_number # payment to date
payment_number += 1
print(payment_number) # payment no.
print(ptd) # amount paid to date
print(CIwoP) # interest for that month
print(x) # balance for each month after payment
递归方法
def remainb(x, payment_number=0):
if x < 0: return
x = (x + (AI / 12) * x) - MP
CIwoP = (x + (AI / 12) * x) - x # interest every month
ptd = MP * payment_number # payment to date
payment_number += 1
print(payment_number) # payment no.
print(ptd) # amount paid to date
print(CIwoP) # interest for that month
print(x) # balance for each month after payment
remainb(x, payment_number)
顺便提一下,使用良好的描述性变量名称而不是 x 是一个好习惯,我 :)
添加回答
举报