1) Formula uses n
, not d
, so why not use n
?
Anyway...
Am I correctly inputting the exponent?
Did you get the output given as shown? You can use a normal calculator to verify those numbers.
That being said, if you read the question carefully, you are initially inputting "annual interest", not r
. The instructions say that r
is annual interest over 12.
So here is that adjustment to your code.
p = annual_interest = n = -1
while True:
p = int(input('Enter the amount of the loan: '))
annual_interest = float(input('Enter the interest rate: '))
n = int(input('Enter the length of loan in months: '))
if p <= 0 or annual_interest <= 0 or n <= 0:
print('Enter a postive number')
else:
break
r = annual_interest / (12 * 100) # this needs to be a decimal percentage
Then, 1/n
is not a negative exponent, it is the N-th root. You can just put a negative exponent in Python, no need to try to think you can re-write it any differently.
monthly_payment = (p * r) / (1 - ((1 + r) ** -n))
interest = n * monthly_payment - p