0

I'm taking the course 6.00.1x Introduction to Computer Science and Programming. I have been asked to come up with a program that calculates the minimum repayments needed to pay off the credit card balance within a year. For this, I need to use bisection methods.

I have come up with this piece of code (please see below). When I run the code, I get 29591.55 as the answer; however, I'm meant to get 29157.09.

Can anyone help me with where I've gone wrong?

balance = 320000
annualInterestRate = 0.2
monthlyInterestRate = annualInterestRate/12.0
lowerRepayment = balance/12
upperRepayment = (balance * (1 + monthlyInterestRate)**12)/12
repayment = (lowerRepayment + upperRepayment)/2
unpaidBalance = balance
while unpaidBalance >= 0:
    repayment = round(((lowerRepayment + upperRepayment)/2), 2)
    unpaidBalance = balance
    numberOfRepayments = 0
    while numberOfRepayments < 12 and unpaidBalance >= 0:
        numberOfRepayments += 1
        monthlyUnpaidBalance = unpaidBalance - repayment
        increment = monthlyUnpaidBalance * monthlyInterestRate
        unpaidBalance = monthlyUnpaidBalance + increment
    if unpaidBalance < -0.01:
        lowerRepayment = repayment
    elif unpaidBalance > 0.01:
        upperRepayment = repayment
print 'Lowest Payment: ' + str(repayment)
  • pick one: http://stackoverflow.com/search?q=[python]bisect+credit+card – roippi Nov 03 '13 at 18:39
  • @roippi - thank you. I've had a look at the other posts and I'm still not getting 29157.09 as the answer. – user2949713 Nov 03 '13 at 18:47
  • 1
    Here's a hint. Whenever `unpaidBalance < 0` the outer `while` loop finishes, regardless of whether the `repayment` amount is the correct one or not. – martineau Nov 03 '13 at 19:01
  • Thank you @martineau. How do I do this? I've tried adding another condition to the outer while loop and it becomes unresponsive. – user2949713 Nov 03 '13 at 19:37
  • In simple terms, it needs to keep looping until `unpaidBalance` _close_ to zero -- over or under. You also have to watch out that it doesn't get into a situation where the repayment value is not longer changing one way or the other each iteration because then it will go into an infinite loop. – martineau Nov 03 '13 at 23:44

1 Answers1

0

you have to change the conditions as if unpaidBalance > 0.01: lowerRepayment = repayment elif unpaidBalance < -0.01: upperRepayment = repayment 2.make your loop end when unpaidBalance is between -0.01 and 0.01