user2066771 user2066771 - 2 months ago 12
Python Question

Using Bisection Search on Lowest Payments on Credit Card debt and

My code:

monthlyInterestRate = annualInterestRate/12.0
low = balance/12
high = (balance*(1+monthlyInterestRate)**12)/12
guess = (low+high)/2
unpaidBalance = balance
month = 1

while True:
unpaidBalance= unpaidBalance-guess
while month < 13:
if unpaidBalance <= -0.1:
low = guess
month += 1
elif unpaidBalance >= 0.1:
high = guess
month += 1
else:
break
guess = (low + high)/2
print "Lowest Payment: " + str(round(guess, 2))


When I test it it gets stuck at the line "while month < 13:"

Why does it do this and how do I fix it?

Answer

If you break at each loop of inner while, you remains less than 13.

And this goes on and on since you proceed While True and do not update your guess.

I fear you are facing infinite looping there.

Your break statement breaks the closest loop, that is the While month < 13 loop. The next line is not read. guessis not updated. The while True is not broken.

Maybe you wanted to say

while month < 13:
   unpaidBalance= unpaidBalance-guess 
   if unpaidBalance <= -0.1:
         low = guess
   elif unpaidBalance >= 0.1:
         high = guess
   month += 1
   guess = (low + high)/2