The problem is to write the python that calculates the interest due on a loan and prints a payment schedule. The interest due on a loan can be calculated according to the simple formula:
I = P × R × T
where I is the interest paid, P is the amount borrowed (principal), R is the interest rate, and T is the length of the loan.
Finally it needs to displayed as follows:
The program will print the amount borrowed, total interest paid, the amount of the monthly payment, and a payment schedule.
Sample session
Loan calculator
Amount borrowed: 100
Interest rate: 6
Term (years): 1
Amount borrowed: $100.00
Total interest paid: $6.00
Amount Remaining
Pymt# Paid Balance
----- ------- ---------
0 $ 0.00 $106.00
1 $ 8.84 $ 97.16
2 $ 8.84 $ 88.32
3 $ 8.84 $ 79.48
4 $ 8.84 $ 70.64
5 $ 8.84 $ 61.80
6 $ 8.84 $ 52.96
7 $ 8.84 $ 44.12
8 $ 8.84 $ 35.28
9 $ 8.84 $ 26.44
10 $ 8.84 $ 17.60
11 $ 8.84 $ 8.76
12 $ 8.76 $ 0.00
The complete problem description is here: http://openbookproject.net/pybiblio/practice/wilson/loan.php To accomplish this I have written the code which is as follows:
import decimal
from decimal import *
class loan_calc:
def __init__(self):
decimal.getcontext().prec = 3
p = Decimal(input('Please enter your loan amount:'))
r = Decimal(input('Please enter the rate of interest:'))
t = Decimal(input('Please enter loan period:'))
r_e = r/100
i = p*r_e*t
term = t*12
r_a = p+i
amnt = p/term
count = 0
b_r = r_a
print "Payment\t\tAmount Paid\t\tBal.Rem."
while count <= term:
if count == 0:
print count,"\t\t"'0.00'"\t\t\t",b_r
count += 1
b_r -= amnt
continue
if term - count == 1:
amnt = b_r
print count,"\t\t",amnt,"\t\t\t",b_r
count += 1
b_r -= amnt
continue
else:
print count,"\t\t",amnt,"\t\t\t",b_r
b_r -= amnt
count += 1
continue
loan = loan_calc()