-1

This is the problem I'm working on

This is my code

p = int(input('Enter the amount of the loan: '))
r = float(input('Enter the interest rate: '))
d = int(input('Enter the length of loan in months: '))
if p > 0 and r > 0 and d > 0:
    m_payment = (p*r)/(1-(1+r) ** 1/d)
    t_interest = d*m_payment-p
else:
    print('Enter a postive number')
print('Monthly payment: ${0:.2f}'.format( m_payment))
print('Total interest paid: ${0:.2f}'.format(t_interest))

Am I correctly inputting the exponent?

Jeremy
  • 11
  • 2

3 Answers3

2

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
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

No. you are not using the exponent correctly. The formula clearly states that you have to use a negative expononent, but you are trying to get the dth-root.

DahliaSR
  • 77
  • 6
0

The problem appears to only be in the handling of the "annual interest rate" as a "monthly interest rate" converted to a decimal percentage, and the exponent should remain as an integer, only negated.

Keeping your code close to as it is:

p = int(input('Enter the amount of the loan: '))
r = float(input('Enter the interest rate: '))
d = int(input('Enter the length of loan in months: '))

mr = (r/100)/12 # convert annual interest rate to a decimal monthly interest rate

if p > 0 and r > 0 and d > 0:
    m_payment = (p*mr)/(1-((1+mr) ** -d))
    t_interest = d*m_payment-p
else:
    print('Enter a postive number')
print('Monthly payment: ${0:.2f}'.format( m_payment))
print('Total interest paid: ${0:,.2f}'.format(round(t_interest))) # given answer appears rounded

demo:

Enter the amount of the loan: 18000
Enter the interest rate: 5.25
Enter the length of loan in months: 60
Monthly payment: $341.75
Total interest paid: $2,505.00
  • No worries, I didn't read it carefully at first either, as you can see it took me a long time to figure it out... and the purpose of that textbook-problem may have just been to solve that. It also helped us all brush up on our basic math concepts too. :) Hope it helped. – chickity china chinese chicken Mar 21 '17 at 23:42