A community for students.
Here's the question you clicked on:
 0 viewing
 11 months 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.
 11 months 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.

This Question is Closed

Ante
 11 months ago
Best ResponseYou've already chosen the best response.0At 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.

seandisanti
 11 months ago
Best ResponseYou've already chosen the best response.1if 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.

seandisanti
 11 months ago
Best ResponseYou've already chosen the best response.1Don'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

bennyboy489
 11 months ago
Best ResponseYou've already chosen the best response.0I 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

acm2140
 11 months ago
Best ResponseYou've already chosen the best response.0Ante, 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?

acm2140
 11 months ago
Best ResponseYou've already chosen the best response.0I 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?

acm2140
 11 months ago
Best ResponseYou've already chosen the best response.0Nevermind, 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.

seandisanti
 11 months ago
Best ResponseYou've already chosen the best response.1For 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

acm2140
 11 months ago
Best ResponseYou've already chosen the best response.0Thanks for the encouraging words! On to the next class!
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.