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.
MIT 6.00 Intro Computer Science (OCW)
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
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.
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.
Not the answer you are looking for? Search for more explanations.
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
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
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?
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?
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.
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
Thanks for the encouraging words! On to the next class!