def pay_off(balance, rate, payment):
months = 0
total = 0
while balance > 0:
interest = round(balance * rate / 12, 2)
balance = balance + interest
pay = min(payment, balance)
balance = round(balance - pay, 2)
total = total + pay
months = months + 1
return months, round(total, 2)
for p in [50, 100, 200]:
months, total = pay_off(1200, 0.1999, p)
print(p, months, total, round(total - 1200, 2))
Python code. Indented lines belong to the function or loop above them. min(payment, balance) makes sure the last payment is not bigger than what is owed.