#Sage script to illustrate loan payment computations for "Money Matters" project in Chapter 2.

#First choose interest rate "r", initial borrowed amount "p0", monthly paymnet "b", and number of payments "payments".

r = 0.03 #Annual interest rate
b = 1726.45 #Monthly payment
payments = 180 #Number of payments
p = [250000] #Initial payment

#Utility function to round to nearest cent, for aesthetics
r2 = lambda x: round(100*x)/100.0

#Loop over months, store balance in list "p":
print("Month", 0, "Balance", p[0])
for i in range(1,payments+1):
    pnext = (1+r/12)*p[i-1] - b #Next monthly balance
    p.append(pnext) #Append it to the list p.
    print("Month", i, "Interest", r2(r/12*p[i-1]), "Balance", r2(pnext))