[Tutor] exercise (while loop)

Danny Yoo dyoo at hashcollision.org
Tue Apr 1 04:10:06 CEST 2014


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


More information about the Tutor mailing list