user224530
user224530

Reputation: 49

Credit card balance exercise in Python

I am supposed to write a simple program to do the following:

Taking the following inputs:

  1. The outstanding balance on the credit card
  2. Annual interest rate as a decimal

I am supposed to print the fixed monthly payment and the number of months (at most 12 and possibly less) it takes to pay off the debt. I'm also supposed to print the balance at the end (likely a negative number).

We are supposed to assume that interest is compounded monthly according to the balance at the start of the month (before the payment for that month is made). The monthly payment must be a multiple of $ 10 and must be the same for all months. It is possible for the balance to become negative as a result of this scheme.

So, monthly interest rate = annual interest rate / 12.0
Updated balance each month = Old balance * (1 + monthly interest rate) - minimum monthly payment

Now, the people who assigned this problem provided a test case for what would be the correct program:

>>>
Enter the outstanding balance: 1200
Enter annual interest rate: 0.18
RESULT
Monthly payment to pay off debt in 1 year: 120
Number of months needed: 11
Balance: -10.05
>>>

This is the program I wrote (I fixed the inputs to the same as above so as to see if the program works as it should:

## ob = float (raw_input ("Enter the outstanding balance:"))
##accir = float(raw_input("Enter annual interest rate:"))

ob = float (1200)
mp = float (0)
n = float (0)

while rb >= 0:
          rb = ob
          mp += 10
          n += 1
          rb = ( rb*(1 + (accir/12.0)**n) -         n*mp)

print "Result:"
print "Monthly payment to pay off debt in one year:" , mp
print "Number of months needed:" , n
print "Balance:" , rb

Seems ok, but when I run it I get this:

>>>
Result:
Monthly payment to pay off debt in one year: 110.0
Number of months needed: 11.0
Balance: - 10.0
>>>

Which when compared to the correct output I wrote earlier above is slightly different.

Sorry for the messy write up, I don't know how to write code in this forum. Also, please note that I didn't write the question and programs exactly as I have them, but more or less the same.

What could the problem be? Thanks.

Upvotes: 3

Views: 1928

Answers (1)

mistermarko
mistermarko

Reputation: 305

One thing should be cleared up to begin with - how to work out the monthly rate from the annual rate. The general compound interest formula is:

At = A0(1 + r)^t

where At is the total amount at time t, A0 was the amount at time 0 and r is the rate. Rearranging for r:

r = (At/A0)^1/t - 1

Notice that At/A0 is in effect the annual rate plus 1 and in this case we want t to be 12. So the way to get the monthly rate is to state the APR as a decimal, add one, substitute it for the above ratio, raise it to one twelth and then subtract one.

https://math.stackexchange.com/questions/902687/bactracking-to-find-compound-interest/902693#902693

Anyway, here's the answer:

def main(rb):
    count = 0
    while rb > 0:
        rb = round(rb*(0.18/12 + 1) - 120, 2)
        count += 1
        #print(count, rb)
    return count, rb

print(main(input('Balance: ')))   #Balance: 1200

I've used the definition of monthly rate you were given because you have to use it even though it should read rb*(1.18**(1.0/12)). 1.0 enforces true division in python2. If either version is run in python3 an int function must be applied to the input; python2 assumes this.

NB If the original balance is greater than 7999 the interest will exceed the minimum payments and the program will enter an infinite loop.

Upvotes: 1

Related Questions