June 2017
Beginner
352 pages
8h 39m
English
Let's add a new line to the main() function which takes the square-root of -1:
def main(): print(sqrt(9)) print(sqrt(2)) print(sqrt(-1))
If we run that, we get a new exception:
$ python3 sqrt.py3.01.41421356237Traceback (most recent call last): File "sqrt.py", line 14, in <module> print(sqrt(-1)) File "sqrt.py", line 7, in sqrt guess = (guess + x / guess) / 2.0ZeroDivisionError: float division
What has happened is that Python has intercepted a division by zero, which occurs on the second iteration of the loop, and raised an exception – a ZeroDivisionError.
Read now
Unlock full access