function to calculate interest rate

Paul Winkler slinkp23 at yahoo.com
Wed Oct 17 17:56:45 EDT 2001


On Wed, 17 Oct 2001 14:13:17 -0700, Chris Duncan
<chrisd at rocketnetwork.com> wrote:

>Does anyone know of a function I can use to calculate the interest rate and
>payment schedule (in months) for a loan?

I wrote a throwaway script which I used to compare a few credit
options.  It's attached. However, I did *not* check to see if this
works out the same as what the bank or card company calculates. It may
well be inaccurate. But for me, approximate answers were good enough
for the situation.

-- Paul Winkler

#!/usr/bin/python

import sys
print """
Time to calculate some interest...
WARNING: results not guaranteed!
This is really just designed to give you a general idea.
"""
print "What is the APR (as a percentage)? ",
apr = sys.stdin.readline() / 100.0
mpr = apr / 12

print "\nWhat is your monthly payment? ",
payment = float(sys.stdin.readline())

print "\nWhat is your beginning balance? ",
balance = float(sys.stdin.readline())

print "-" * 50
print "SCHEDULE..."
print "-" * 50

i = 1
begin_balance = balance
end_balance = balance
total_interest = 0.0
total_paid = 0.0

while end_balance > 0.0:
    print "MONTH", `i`

    interest = mpr * begin_balance
    # Don't overpay.
    if payment <= begin_balance + interest:
        real_payment = payment
    else: real_payment = begin_balance + interest
    
    end_balance = begin_balance + interest - real_payment

    print "\tending balance:"
    print "\t %f + %f - %f = %f" % \
          (begin_balance, interest, real_payment, end_balance)
    ## set up next month.
    i = i + 1
    begin_balance = end_balance
    
    ## count totals.
    total_paid = total_paid + real_payment
    total_interest = total_interest + interest

print "TOTAL PAID: %f\tTOTAL INTEREST PAID: %f" % (total_paid, total_interest)






More information about the Python-list mailing list