Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

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 

Made multiples attempts at debugging but failed repeatedly for hours. Frankly, I am stuck. If anyone can give me any advice on how to approach this problem (eg. run the loop until SA==0).

1 Answer

0 votes
by (36.8k points)

You are trying to Switching to while loop is more appropriate as you are not sure how many times you need to loop. This will continue as long as x greater than 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

Recursion approach

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)

 If you are a beginner and want to know more about Python the do check out the Python Data Science Course.

Browse Categories

...