## acm2140 one year ago Hi all, I am stuck on ps 1 c. Printing it out it seems to get stuck in a neverending loop where newBalance > 0 but I don't seem to be changing the monthlyPayment like I hoped I was. Can someone give me some hints as to how to solve this bisection search problem? I feel like I understand it in theory but am unable to make it work.

1. acm2140

Here is my code

2. Ante

At first glance, you might wanna check that for loop. Seems like you're making it pretty certain that it will terminate after the first iteration.

3. seandisanti

if you add print 'upper' + str(upperPayment) + ' lower' + str(lowerPayment) as the first line of your loop, you'll see that you very quickly run into a situation where your upper and lower bounds are the same, at which point adding them together and dividing by 2 results in the same answer. Such begins your infinite loop. Also as Ante stated, you're breaking on every iteration of your for loop regardless of the comparison. You're very close to getting this, so I don't want to ruin it for you, but look at your code and see why your upper and lower bounds would run into each other. also step through your logic to make sure that you're doing what you think you're doing.

4. seandisanti

Don't forget about your epsilon value either, and the algorithm on this one is simple enough that if necessary, you could do the problem manually on paper or in excel to see the steps necessary vs the steps being done. That's a worst case scenario of course, but you can do it

5. bennyboy489

I believe it is in this line of code here: while abs(balance) >= epsilon and monthlyPayment <= x: inside the while loop, you have nothing changing the variable to make it stop, so when it ends through all the ifs/else, etc, it goes back to check if that statement is still true or false, it then goes through it again and again until you kill it

6. acm2140

Ante, Sean, Benny: Thanks for your helpful tips!! And for not just telling me the answer... I realized a couple of mistakes I had made: 1. Using the print statement Sean suggested I realized that due to missing parentheses (!) the lower and upper bounds were actually set to the same value from the start!! 2. My newBalance wasn't updating at all (yay print debugging) 3. My while loop was set to check for balance, not newBalance and was also therefore doomed to fail. Now my code is working but it isn't returning the exact answers in the test code for the problem set. The examples in the problem set suggest that the remaining balance should be a small negative value (first example -0.01, second example -0.12). Mine is returning exact answers with no remainder, but seemingly correct answers. Can someone explain why my answer is slightly different from the examples in the problem set?

7. acm2140

I might add that I was wondering if it has anything to do with the type of value printed (string vs float)? Could that be the case?

8. acm2140

Nevermind, I just found the solution to the problem set and see that my code wasn't nearly as sophisticated when it came to rounding at different parts during the process. A bit disheartening to see that I was so far off from the solution code even though my answers were extremely close.

9. seandisanti

For any problem, especially programming ones, there are a LOT of solutions, and it's only possible to learn from attempts that we fail at. You made progress and hopefully learned something in analyzing the differences in your code, and your efforts to do it yourself. Stick with it

10. acm2140

Thanks for the encouraging words! On to the next class!