# overflow error python 3 mortgage calculator

Edwin Meriaux Source

I have a program that has an overflow error every time I use it. This is a mortgage interest calculator program.

global t
t = 0
def time_until_last_payment():
global a
global b
global c
global d
a = float(input('what is the current mortage on the house? '))
b = float(input('what is the current interest rate? please give it in percent but do not add the percent sign. '))
c = float(input('what is the monthly payment? '))
d = float(input('how many installments do you pay a year? '))
b = b/100
while a > 0:
global t
a = (a*(1+b/d)**(d*t/12))+a
a = a - c
t = t + 1
print(t)

time_until_last_payment()

and the overflow error is on the line:

a = (a*(1+b/d)**(d*t/12))+a
pythonpython-3.x

answered 5 months ago BoarGules #1

The overflow error is because your accrual calculation is wrong. The formula is incorrect, in other words, and the correct one is much simpler. Every month t the outstanding capital a increases by annual interest b divided by interest periods per year d:

a = a*(b/d) + a

before being reduced by the payment c:

a = a - c

The formula in your code appears to confuse calculation of compound interest over multiple periods with what your program is trying to do, which is to calculate simple interest for one period.

When you get an error like this, the most useful approach is to print out intermediate results by putting a print() call just after the calculation. That would have shown you at once that your value of a was increasing wildly instead of decreasing slowly as you might expect.

The error message even pointed out to you the line that was wrong. When that happens you need to check the calculation by hand, and with a program with only one such calculation, that is very quick to do.

Your prompt for "monthly payment" includes the assumption that the answer to the next question, "installments per year" will always be 12.