I'm currently working on the third problem in the first problem set. I also got my script running, however if I enter the first test case, my answer is 29643.04 instead of .05 and my balance is still positive .005 something. How can I avoid the script from stopping while the balance is still positive?
I'm following the 2011 spring course btw and posted my script here: http://dpaste.com/1232297/
a quick answer would be: while balance is positive: do stuff if you are getting a wrong answer - print out variable values and calculation results at strategic points of the code to see if what is really happening is what you expected to happen comments: at the bottom of the loop (line 25) you change ans - if you then 'drop out' of the loop, ans might be different than the ans that caused it to 'drop out' suggest moving line 25 to the top of the loop and deleting line 6.
Thanks; your comments did clean up my code a bit. I already printed out some variable values and calculation results at strategic points and noticed two possible errors: (1) Apparently my upper bound gets too small at some point, so that the 'middle' point can never be the right answer anymore, and (2) The script ends when the balance (i.e. 'remaining' variable) reaches 0.005 something (i.e. smaller than 'epsilon', but still positive) instead of ending when the balance is at most 0. I'm not quite sure what stuff to do in order to avoid this, especially considering my upper bounds is already smaller than my answer should be. I tried to change line 13 into: while abs(remaining) > epsilon or remaining > 0: This changed my answer a bit (it made the balance -0.03 something) but the (rounded) 'ans' remained exactly the same. Makes sense?
If your bounds 'bypass' the answer then your condition for stopping must not be working if your condition is 0 and yur tolerance is -epsilon (instead of +/- epsilon) try: -epsilon < remaining <= 0 " but the (rounded) 'ans' remained exactly the same" do you understand why that can be possible?
Yeah, I figured that the real ans is probably different so that it will leave a different 'remaining', but it still rounds to the same number. Thanks again for your reply. Where do you propose I put the improved condition? Behind my last 'if'? See http://dpaste.com/1234883/
Join our real-time social learning platform and learn together with your friends!