Q:

Newton’s method for finding square roots in python

This is a function that uses Newton's method to solve for the square root of a given number:
def sqrt(n):
    approx = n / 2.0
    while True:
        better = (approx + n / approx) / 2.0
        if abs(approx - better) < 0.00001:
            return better
        approx = better

What I do not understand is, that the function only returns "better", which is a integer, but the while loop depends on a Boolean, so how is the while loop ever going to stop? Where does the function returns a False to terminate the while loop?
Many thanks in advance.

A:

Actually, it never "returns a False to terminate the while loop" because it simply doesn't need to.
When Python hits a return-statement inside a function, it immediately exits that function.  Meaning, when return better is reached, sqrt is exited and, in the process, so is the while-loop.

