TransWikia.com

Interest calculator in Python. Find total sum and how much to pay each year

Stack Overflow Asked by Kat on December 14, 2020

My goal is to find the total sum of the loan after five years, and also how much to pay each year.

My code so far:

years = 5

loan = 50000

interest = 0.05


for year in range(years):
    loan += loan * interest
    print(loan)

And is this the right way to find the annual payment per year?

sum = loan / years + loan * interest

One Answer

If you don't want to do the calculations yourself, you could make use of numpy.

>>> import numpy as np
>>> np.pmt(.05,5,-50000)
11548.739906413395

The above gives the annual payment.

So, for the first year, the amount of principal and interest paid would be:

interest = 50000*.05

principal_paid = 11548.74 - interest

Here is a small program that does that.

import numpy as np

interest = .05
principal = 50000
years = 5

annual_pay = np.pmt(interest,years,-principal)

print('{}{:>10}{:>10}{:>10}'.format('year','interest','retired', 'balance'))

for yr in range(1,6):
    i = interest * principal
    retired_prin = annual_pay - i
    principal = principal - retired_prin
    print('{:>4}{:>10.2f}{:>10.2f}{:>10.2f}'
          .format(yr, i, retired_prin, principal))
    

This prints:

year  interest   retired   balance
   1   2500.00   9048.74  40951.26
   2   2047.56   9501.18  31450.08
   3   1572.50   9976.24  21473.85
   4   1073.69  10475.05  10998.80
   5    549.94  10998.80     -0.00

Correct answer by Chris Charley on December 14, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP