Max Lin
Max Lin

Reputation: 3

Python simple interest calculation

I am trying to calculate how much monthly payment in order to pay off a loan in 12 month. use $10 as incremental.

Payment = 0
balance = float (1200)
interest = float (0.18)
MonthlyInt = interest/12.0

while balance > 0 :

    Payment = Payment + 10
    month = 0
    while month < 12 and balance > 0:
        IntPay = balance* MonthlyInt
        balance += IntPay
        balance -= Payment
        month += 1
print Payment

The correct answer should be 110, why am I getting 60?

Upvotes: 0

Views: 1059

Answers (3)

VISHAKHA AGARWAL
VISHAKHA AGARWAL

Reputation: 1

Q..

Write a Python program to input principal amount, rate of interest and number of years with appropriate prompts. Find simple interest and display all the details in the following format:
Principal Amount : Rs. __________
Rate of Interest : ____ %
Number of Years : ____
Simple Interest : Rs. __________
Maturity Amount : Rs. __________

ANSWER::

# for input
p = float (input("ENTER PRINCIPLE AMOUNT : "))
r = float (input("ENTER RATE OF INTEREST : "))
y = float (input("ENTER NUMBER OF YEARS : "))
# SIMPLE INTEREST
si = (p * r * y)/100
# display
print("{0:25}: Rs. {1}".format("Principal Amount", p))
print("{0:25}: {1}".format("Rate Of Interest",r ),"%")
print("{0:25}: {1}".format("Number Of Years",y))
print("{0:25}: Rs. {1}".format("Simple Interest",si))
print("{0:25}: Rs. {1}".format("Maturity Amount",p + si))

Upvotes: 0

stephen barter
stephen barter

Reputation: 421

Here is another way of doing it. I tried using Brians example in the MIT edx course and was unable to get it to work in all cases.

this is how I did it.

def lowestpayment(x,y):
""" x = total balance due
    y = annual interest rate 
    Returns min payment needed to pay off debt in 1 year
"""
month = 0
balance = x
annualInterestRate = y
payment = 0
while balance > 0 and month <= 12:
    month = 0
    balance = x
    payment += 10
    balance = balance - payment
    monthlyint = balance*annualInterestRate/12
    balance += monthlyint
    month += 1
    if balance+(balance*annualInterestRate/12)*11-payment*12 <= 0:
        return payment


print("Lowest Payment: ",lowestpayment(35,0.25))

Upvotes: 1

Brian
Brian

Reputation: 1998

The main things generating the difference are:

  • The balance should be reset to 1200 before looping through the 12 months again
  • The payment should be deducted from the balance before calculating the interest

A couple smaller python things are:

  • float() isn't needed around numbers like 0.18, it's already a float
  • 1200. would imply that the number is a float, so float() isn't needed

Accounting for these things then:

Payment = 0
interest = 0.18
MonthlyInt = interest/12.0
balance = 1200.

while balance > 0 :

    Payment = Payment + 10
    month = 0
    balance = 1200.
    while month < 12 and balance > 0:
        balance -= Payment
        IntPay = balance* MonthlyInt
        balance += IntPay
        month += 1
print(Payment)

gives a result of 110.

Upvotes: 3

Related Questions