Jorge de Paula Jr
Jorge de Paula Jr

Reputation: 27

Calculating how long it will take to pay off a loan

I'm stuck in my exercise about how long it will take to pay off a loan. I'm trying to do a loop calculating the interest and new balance after each monthly payment. Here what's I have so far:

def conta ():
  total = float(input('How much do you want to borrow?'))
  qtoPg = float(input('What is the monthly payment amount?'))
  annualInterest = float(input('What is the annual interest rate expressed as a percent?'))
  rateMonth = (annualInterest*0.01)/12
  tjm = total*rateMonth
  nbalance = total+tjm-qtoPg
  while nbalance <= qtoPg:
    tjm = nbalance*rateMonth
  print (nbalance)

Upvotes: 1

Views: 863

Answers (2)

Subhashis Pandey
Subhashis Pandey

Reputation: 1538

This should use EMI concept. Add to total amount the monthly interest and subtract the EMI amount paid per month. When the balance amount is less than or equal to zero then break the loop.

The below modified function will print the number of EMI's to be paid.

def conta ():
  
  total = float(input('How much do you want to borrow? '))
  qtoPg = float(input('What is the monthly payment amount? '))
  annualInterest = float(input('What is the annual interest rate expressed as a percent? '))
  
  rateMonth = (annualInterest*0.01)/12
  
  nbalance = total
  emis = 0
  
  while nbalance > 0:
    emis += 1
    nbalance = nbalance + (rateMonth*nbalance)
    
    nbal = qtoPg
    if nbalance < qtoPg:
      nbal = nbalance
    
    nbalance -= qtoPg

    if nbalance <= 0:
      print(nbal)
    
  print (emis)

Upvotes: 0

Michael Lorton
Michael Lorton

Reputation: 44406

I don’t see how that is even supposed to work.

You have a loop where the exit condition compares nbalance and qtoPg but inside the loop, those values are not changed.

Also, your basic math is wrong. Each month, the balance goes down by the payment-amount, but it goes up by the interest due, which is based on the balance, not the original principal.

Upvotes: 2

Related Questions