运行直到贷款余额达到 0 的递归函数的语法问题

SA=1000.00

AI=0.12

MP=100.00

def remainb(x):

if x==0:

    return 0

else: 

    return

    x=(SA+(AI/12)*SA)-MP

    for i in range(x,1000000):

        x=(x+(AI/12)*x)-MP

        CIwoP=(x+(AI/12)*x)-x #interest every month

        ptd=MP*i#payment to date

        #ptdreal=(ptd-CIwoP)

        #rbal=(CIwP-ptd)

        print(i)#payment no.

        print(ptd)#amount paid to date

        print(CIwoP)#interest for that month

        print(x)#balance for each month after payment

        #if rbal==0: return 0

        #return 

多次尝试对此进行调试,但数小时内反复失败。坦率地说,我被困住了。如果有人能就如何解决这个问题给我建议(例如,运行循环直到 SA==0),我将永远感激不已。先感谢您。


阿晨1998
浏览 137回答 1
1回答

繁华开满天机

我认为这就是您要实现的目标。切换到 while 循环更合适,因为您不确定需要循环多少次。只要 x 大于 0,这种情况就会继续。循环方法x = (SA + (AI / 12) * SA) - MPpayment_number = 0while x > 0:&nbsp; &nbsp; x = (x + (AI / 12) * x) - MP&nbsp; &nbsp; CIwoP = (x + (AI / 12) * x) - x&nbsp; # interest every month&nbsp; &nbsp; ptd = MP * payment_number&nbsp; # payment to date&nbsp; &nbsp; payment_number += 1&nbsp; &nbsp; print(payment_number)&nbsp; # payment no.&nbsp; &nbsp; print(ptd)&nbsp; # amount paid to date&nbsp; &nbsp; print(CIwoP)&nbsp; # interest for that month&nbsp; &nbsp; print(x)&nbsp; # balance for each month after payment递归方法def remainb(x, payment_number=0):&nbsp; &nbsp; if x < 0: return&nbsp; &nbsp; x = (x + (AI / 12) * x) - MP&nbsp; &nbsp; CIwoP = (x + (AI / 12) * x) - x&nbsp; # interest every month&nbsp; &nbsp; ptd = MP * payment_number&nbsp; # payment to date&nbsp; &nbsp; payment_number += 1&nbsp; &nbsp; print(payment_number)&nbsp; # payment no.&nbsp; &nbsp; print(ptd)&nbsp; # amount paid to date&nbsp; &nbsp; print(CIwoP)&nbsp; # interest for that month&nbsp; &nbsp; print(x)&nbsp; # balance for each month after payment&nbsp; &nbsp; remainb(x, payment_number)顺便提一下,使用良好的描述性变量名称而不是 x 是一个好习惯,我 :)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python