Skip to content Skip to sidebar Skip to footer

Credit Card Balance Exercise In Python

I am supposed to write a simple program to do the following: Taking the following inputs: The outstanding balance on the credit card Annual interest rate as a decimal I am suppos

Solution 1:

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:

defmain(rb):
    count = 0while 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.

Post a Comment for "Credit Card Balance Exercise In Python"