DevNet Associate DEVASC 200-901 Official Certification Guide by Adrian Iliesiu (z-lib.org)

(andrew) #1
-----------------------
Ran 2 tests in 0.001s
FAILED (failures=1)

The first check is still good, so you see one dot at the top,
but next to it is a big F for fail. You get a message saying
that the test_value function is where it failed, and see
that your original function did not catch this error. This
means that the code is giving bad results. A radius of -1 is
not possible, but the function gives you the following
output:


>>> area_of_circle(-1)
3.141592653589793

To fix this, you go back to your original function and
some error-checking code. You use a simple if statement
to check for a negative number, and you raise a
ValueError with a message to the user about invalid
input:


Click here to view code image


from math import pi

def area_of_circle(r):
if r < 0:
raise ValueError('Negative radius value
error')
return pi*(r**2)

Now when you try the test from the interpreter, you see
an error raised:


Click here to view code image

Free download pdf