0

Hi all, i am trying to write a mortgage amortization schedule calculator and am stuck. please see the code below. It appears that the principal is being counted twice before subtracting it from the balance. Any ideas on what the issue is? I am new to python:

```
loanAmount = input("Please enter the loan amount: ")
iR = input("Please enter the interest rate: ")
iR = float(iR)
term = input("Please enter the term: ")
monthlyRate = iR/(12 * 100)
loanTerm = term*12
monthlyPayment = loanAmount * (monthlyRate/(1 - (1 + monthlyRate)** -loanTerm))
monthlyPayment = float(monthlyPayment)
monthlyPayment = round(monthlyPayment, 2)
interest = loanAmount*monthlyRate
balance = loanAmount - monthlyRate
print "PmtNo\t\tInterest\tPrincipal\tBalance"
for i in range (1, loanTerm + 1):
pi = balance*monthlyRate
monthlyPayment = loanAmount * (monthlyRate/(1 - (1 + monthlyRate)** -loanTerm))
prepay= monthlyPayment - pi
balance = loanAmount - prepay
remaining_balance = balance - prepay
if balance < 0:
pi = balance*monthlyRate
balance = loanAmount - prepay
prepay = monthlyPayment - pi
remaining_balance = balance - prepay
print "%d\t\t%0.2f\t\t%0.2f\t\t%0.2f" % (i, pi, prepay, 0)
break
print "%d\t\t%0.2f\t\t%0.2f\t\t%0.2f" % (i, pi, prepay, remaining_balance)
```

*Edited
by rednose00*: code added twice