Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 16 Online
OpenStudy (anonymous):

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.

OpenStudy (anonymous):

Here is my code

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

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

OpenStudy (anonymous):

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

OpenStudy (anonymous):

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?

OpenStudy (anonymous):

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?

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

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

OpenStudy (anonymous):

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

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!