Re: [Tutor] exercise (while loop)
> So this call will always try to round None(the default return value) > And of course it produces no output since it prints nothing. > > Are you sure that's actually what is in the book? No. That's very much why I wanted a reference to the original source of the problem. Scott attributed too much to the book when he presented the problem. In the original content, http://greenteapress.com/thinkpython/html/thinkpython008.html#toc81 it simply presents a running dialogue exploring the idea of computing square roots iteratively, culminating in a toplevel for-loop that simply prints out its improving guess. There is no function there. This is why we want to be a bit more careful when saying "The book said this..." following up with a paraphrase, because sometimes we can get the paraphrasing wrong. Similarly issues occur when one is presenting error message content and asking for debugging advice. Pointing to primary sources is usually a good idea, especially when debugging or trying to get at root causes. Let's head-off this sort of confusion quickly next time. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exercise (while loop)
On 01/04/14 02:07, Scott W Dunning wrote: I’m working on a few exercises and I’m a little stuck on this one. This is what the book has but it just gives me an endless loop. def square_root(a, eps=1e-6): while True: print x y = (x + a/x) / 2 if abs(y-x) < epsilon: break Are you sure that's what the book has? If so I'd consider another book. That code is seriously broken. - It prints x before an x is defined. - It never modifies x and so the abs(y-x) will always be the same so the loop never breaks. - it tests for epsilon but has eps as parameter - It also never returns any value from the function. round(square_root(9)) So this call will always try to round None(the default return value) And of course it produces no output since it prints nothing. Are you sure that's actually what is in the book? I tweaked it to what I thought was correct but when I test it I get nothing back. def square_root(a, eps=1e-6): x = a/2.0 while True: y = (x + a/x)/2.0 if abs(x - y) < eps: return y x = y This is slightly better than the above, at least it creates an x and modifies it and returns a value. And it seems to work on my system. How did you test it? round(square_root(9)) If you used the >>> prompt this would have produced a result but if you used a script file you would need to print it. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exercise (while loop)
Scott W Dunning Wrote in message: > Iâm working on a few exercises and Iâm a little stuck on this one. > > This is what the book has but it just gives me an endless loop. > > def square_root(a, eps=1e-6): > while True: > print x > y = (x + a/x) / 2 > if abs(y-x) < epsilon: > break > Without an initial value for x, this should give an immediate exception. Assuming you fix that as below, you now have the problem that they never change x, so if it isn't right on first loop, it never will be. Next you have the problem of inconsistent spelling of eps. And final thing I notice is that it doesn't return a value. Once you remove the debug print in the function, there's no way to see the result. > round(square_root(9)) > > I tweaked Good job, you fixed most of the bugs. > it to what I thought was correct but when I test it I get nothing back. > > def square_root(a, eps=1e-6): >x = a/2.0 >while True: >y = (x + a/x)/2.0 >if abs(x - y) < eps: >return y >x = y > > round(square_root(9)) > > The way I tweaked it seems to work, Iâm getting the correct answer on the > calculator but the interpreter is not returning anything when I check in > python. Sure it is, you're just not printing it. You forgot to save the result of rounding, and forgot to print the saved value. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exercise (while loop)
On 01/04/2014 02:47, Danny Yoo wrote: On Mar 31, 2014 6:22 PM, "Scott W Dunning" mailto:scott@cox.net>> wrote: > > I’m working on a few exercises and I’m a little stuck on this one. > > This is what the book has but it just gives me an endless loop. > > def square_root(a, eps=1e-6): > while True: > print x > y = (x + a/x) / 2 > if abs(y-x) < epsilon: > break > > round(square_root(9)) Hi Scott, Ah. I think I see what might be wrong, but let's make sure about this. Can you explain what 'x', 'y' are in this function? And the difference between eps and epsilon while (ouch) we're at it. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exercise (while loop)
On Mar 31, 2014, at 7:10 PM, Danny Yoo wrote: Thanks for the info Danny! I’ll try that and I should be able to figure it out with your help! The book I was referring to is greentreepress. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exercise (while loop)
On Mon, Mar 31, 2014 at 8:48 PM, Scott W Dunning wrote: > > On Mar 31, 2014, at 7:10 PM, Danny Yoo wrote: > Thanks for the info Danny! I’ll try that and I should be able to figure it > out with your help! > > The book I was referring to is greentreepress. The reason I'm asking is I want to double check the example code. Checking... http://greenteapress.com/ ... but Green Tree Press publishes a few Python books. Hmmm. I will guess that you mean: Allen Downey's: "How to Think Like a Computer Scientist". Ah, found it. http://greenteapress.com/thinkpython/html/thinkpython008.html#toc81 But please, try to provide details. You tend to suppress helpful details. I would like to avoid guessing next time, so be aware that we don't see what you're thinking. Ok, I see now what you were looking at. But we need to wheel back around to one of your original questions. You said: > This is what the book has but it just gives me an endless loop. > > def square_root(a, eps=1e-6): > while True: > print x > y = (x + a/x) / 2 > if abs(y-x) < epsilon: >break > > round(square_root(9)) Go back and look at that text again: http://greenteapress.com/thinkpython/html/thinkpython008.html#toc81 and now see that the book does not present a function in that section. Instead, it's showing exploratory code. There's no function there, all the state is global, and it's not computing a return value. So you shouldn't be too surprised that the code the book is presenting, as a non-functional example, requires some adaptation before it works as a function. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exercise (while loop)
> I tweaked it to what I thought was correct but when I test it I get nothing > back. > > def square_root(a, eps=1e-6): >x = a/2.0 >while True: >y = (x + a/x)/2.0 >if abs(x - y) < eps: >return y >x = y > > round(square_root(9)) > > The way I tweaked it seems to work, I’m getting the correct answer on the > calculator but the interpreter is not returning anything when I check in > python. I didn't want to keep you waiting, so I'll cut to the chase. This line here in your program: round(square_root(9)) computes a value... But it doesn't do anything with that value. Try printing the value. You may also try to see that your program is doing something effective by "unit testing" it. This is often a lot better than just printing values and looking at them, because the test case will say what the _expected_ value is, so it's more informative. For this example, the following is a start at unit testing the above function. Add the following to the bottom of your program's source. ### ## See: http://www.openp2p.com/pub/a/python/2004/12/02/tdd_pyunit.html import unittest class SquareRootTests(unittest.TestCase): def testSimpleCases(self): self.assertAlmostEqual(square_root(1), 1.0) self.assertAlmostEqual(square_root(4), 2.0) if __name__ == '__main__': unittest.main() ### Here's what it looks like when I run this: ## $ python sqrt.py 4.472135955 . -- Ran 1 test in 0.000s OK ## You can then start adding more and more to tests to gain confidence that the code is doing something reasonable. If we try to put in an intentionally broken test, like: self.assertAlmostEqual(square_root(3), 2.0) in the body of testSimpleCases(), then we'll see the following error when running the program: ## $ python sqrt.py 4.472135955 F == FAIL: testSimpleCases (__main__.SquareRootTests) -- Traceback (most recent call last): File "sq.py", line 20, in testSimpleCases self.assertAlmostEqual(square_root(3), 2.0) AssertionError: 1.7320508075688772 != 2.0 within 7 places -- Ran 1 test in 0.000s FAILED (failures=1) ## And that's what you want to see. If either the test or the code is bad, it'll say something about it. One other thing: you will want to check a particularly insidious case that will cause the program here to behave badly. Consider the zero case: square_root(0). Write the test case. Run it. You'll see something interesting. Good luck! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exercise (while loop)
Also, which book? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exercise (while loop)
On Mar 31, 2014 6:22 PM, "Scott W Dunning" wrote: > > I’m working on a few exercises and I’m a little stuck on this one. > > This is what the book has but it just gives me an endless loop. > > def square_root(a, eps=1e-6): > while True: > print x > y = (x + a/x) / 2 > if abs(y-x) < epsilon: > break > > round(square_root(9)) Hi Scott, Ah. I think I see what might be wrong, but let's make sure about this. Can you explain what 'x', 'y' are in this function? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor