Odez
Odez

Reputation: 21

Amortization Schedule on Python

I have been working on an amortization schedule using python. however, when I run the code the output does not give me the correct results, instead it appears is looping and printing the same values across, 360 times. Below is the code I wrote.

import pandas as pd
import math

p = float(input("Amount borrowed: "))
r = float(input("Annual Interest Rate: "))
t = float(input("Payback Period in years: "))

m=(p*(r/12)*(math.pow(1+r/12,12*t)))/(math.pow(1+r/12,12*t)-1)
print()
print("Your payment will be: $"+str(m))
print()
print("Month\tStartingBalance\tInterestCharge\tPayment\tEndingBalance")
month=12*t
month=int(month)
startingBalance=p
for i in range(1,month+1):
  interestCharge=r/12*startingBalance
  endingBalance=startingBalance+interestCharge-m
  print(str(i)+"\t$"+str(round(startingBalance,2))+"\t$"+str(round(interestCharge,2))+"\t$"+str(round(m,2))+"\t$"+str(round(endingBalance,2)))

Output

Upvotes: 1

Views: 547

Answers (2)

miroslav licko
miroslav licko

Reputation: 81

To get an update code needs

startingBalance = endingBalance

Modified example with input data according https://www.investopedia.com/terms/a/amortization_schedule.asp

import pandas as pd
import math

#p = float(input("Amount borrowed: "))
#r = float(input("Annual Interest Rate: "))
#t = float(input("Payback Period in years: "))

# sample data entered instead
p = float(250e3)
r = float(4.5*.01)
t = float(30)


m=(p*(r/12)*(math.pow(1+r/12,12*t)))/(math.pow(1+r/12,12*t)-1)
print()
print("Your payment will be: $"+str(m))
print()
print("Month\tStartingBalance\tInterestCharge\tPayment\t\tEndingBalance")
month=12*t
month=int(month)
startingBalance=p
for i in range(1,month+1):
  interestCharge = r/12*startingBalance
  endingBalance = startingBalance+interestCharge-m
  argI  = str(i)
  argSB = str(round(startingBalance,2))
  argIC = str(round(interestCharge,2))
  argM  = str(round(m,2))
  argEB = str(round(endingBalance,2))
  msg = argI + "\t$" + argSB + "\t\t$" + argIC + "\t\t$" + argM + "\t\t$"+argEB
  print(msg)
  startingBalance = endingBalance

I hope it helps you. Have a fun with

Upvotes: 0

psarka
psarka

Reputation: 1842

All is fine with your code, it just so happens, that both m and interestCharge are equal to 250 000, so your endingBalance never changes. The problem is likely with your math.

Upvotes: 1

Related Questions