[Tutor] exercise (while loop)

Danny Yoo dyoo at hashcollision.org
Tue Apr 1 06:19:21 CEST 2014

On Mon, Mar 31, 2014 at 8:48 PM, Scott W Dunning <scott.w.d at cox.net> wrote:
> On Mar 31, 2014, at 7:10 PM, Danny Yoo <dyoo at hashcollision.org> 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.



... 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

Ah, found it.


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:


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.

More information about the Tutor mailing list