elm95
elm95

Reputation: 13

I Think I'm on target but why is there an invalid identifier character?

(This is Python. So sorry for missing that.)

I'm trying to develop a program that calculates the monthly mortgage payments for a given loan amount, term(in number of years) and range of interest rates from 3% to 18%. The formula I was given was:

D = ((1 + r)^(n) – 1)/r(1 + r) ^ n

where A is the the original loan amount, D is the discount, N is the number of payments times 12, and R is the rate as a decimal divided by 12. I understand how the math should work, but how exactly is this all formatted so I can write a bit python code?

I'll get it to print out something like:

interest rate: 3%. Monthly payment: 1475.61. interest rate: 4%. Monthly payment: 1670.95. interest rate: 5%. Monthly payment: 1878.88. interest rate: 6%. Monthly payment: 2098.43. and so on down to 18%.

My code so far (that second "1" in the last line is giving me problems by being an invalid character in identifier:

#User input is collected
print('enter your loan amount')
a1 = (int(input('amount')))

print ('enter the number of years of the loan')
n1 = (int(input('amount')))

print ('enter the interest rate in decimal format')
r1 = (float(input('rate')))

#Then do the math
n1 * 12 = n

r1 / 12 = r



((1 + r) ** (n) – 1) / r * (1 + r) ** n = D

Upvotes: 0

Views: 190

Answers (2)

Aswin Murugesh
Aswin Murugesh

Reputation: 11070

I think you just started to know the word programming. You can never do

n1*12 = n
r1/12 = r

It should only be

n = n1 * 12
r = r1 / 12

The assigned variable should always be to the left.

Upvotes: 1

Tim Pierce
Tim Pierce

Reputation: 5664

With respect, I'm not sure that you understand the math either. Or maybe I don't. The formula you give doesn't even include a term "A", so I don't understand what "where A is the original loan amount" means, or what the "discount" is supposed to be.

The classic mortgage loan payment formula is:

M = P [ r(1+r)^n ] / [ (1+r)^n - 1 ]

where

  • P is the principal amount on the loan
  • r is the monthly interest rate
  • n is the number of payments

So once you have gotten these values into variables P, r and n, a Python expression to calculate the monthly mortgage payment M would be:

M = P * ( r * (1 + r) ** n ) / ((1 + r) ** n - 1)

Upvotes: 0

Related Questions