Max Lin - 3 years ago 254
Python Question

# 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?

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download