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.