In: Computer Science
Complete the following functions. You MUST, MUST, MUST add:
1) a doc comment in the proper location for EACH function that
describes
the basic purpose of the function.
2) AT LEAST 5 doc test comments in EACH function that:
* test that the function does what it is supposed to do
* tests that it does what it's supposed to do with odd inputs
* tests "edge" cases (numbers at, just above, just below min/max,
empty strings, etc.)
You MUST, MUST, MUST then test each of your methods by BOTH:
1) running the "main.py" script
2) running this module directly to run the doc tests
Except as noted, you can implement the functions however you
like. And if your grade-school
math's out of date, Google's your friend for formulas (BUT NOT FOR
CODE).
CHALLENGE: Use try/except blocks to avoid crashes when passing in unexpected parameters.
circle_area
-----------
RETURN the area of a circle with a radius supplied by the
parameter.
Note that you MUST use the "pi" constant from the math module,
so
use an import statement. If the radius passed is less than 1
or
greater than 1000, PRINT "ERROR" and RETURN 0.
sphere_surface_area
-------------------
RETURN the surface area of a sphere with the supplied radius.
Slightly
different error check here: if the radius passed is less than 1
or
greater than 250, PRINT "ERROR" and RETURN 0.
sphere_volume
-------------
RETURN the volume of a sphere with the supplied radius. Again,
slightly
different error check here: if the radius passed is less than 1
or
greater than 100, PRINT "ERROR" and RETURN 0.
'''
# IMPORT THAT PI CONSTANT HERE
def circle_area(radius):
def sphere_surface_area(radius):
def sphere_volume(radius):
if __name__ == "__main__":
import doctest
doctest.testmod()
from doctest import testmod
import math
#Function to return area of circle.
def circle_area(radius):
'''
Defining input and expected output:
Testing the function do what it supposed to do.
>>> circle_area(4)
50.26548245743669
Testing the function at odd inputs.
>>> circle_area(-20)
ERROR
0
>>> circle_area(1020)
ERROR
0
Testing the function at edge cases.
>>> circle_area(0)
ERROR
0
>>> circle_area(2)
12.566370614359172
>>> circle_area(999)
3135312.609875267
>>> circle_area(1001)
ERROR
0
'''
if(radius<1 or radius>1000):
print("ERROR")
return 0
else:
return ((math.pi)*(radius**2))
#Function to return the surface area of sphere.
def sphere_surface_area(radius):
'''
Defining input and expected output:
Testing the function do what it supposed to do.
>>> sphere_surface_area(4)
201.06192982974676
Testing the function at odd inputs.
>>> sphere_surface_area(-20)
ERROR
0
>>> sphere_surface_area(270)
ERROR
0
Testing the function at edge cases.
>>> sphere_surface_area(0)
ERROR
0
>>> sphere_surface_area(2)
50.26548245743669
>>> sphere_surface_area(249)
779127.544460883
>>> sphere_surface_area(251)
ERROR
0
'''
if(radius<1 or radius>250):
print("ERROR")
return 0
else:
return (4*math.pi*(radius**2))
#Function to return volume of sphere.
def sphere_volume(radius):
'''
Defining input and expected output:
Testing the function do what it supposed to do.
>>> sphere_volume(4)
268.082573106329
Testing the function at odd inputs.
>>> sphere_volume(-20)
ERROR
0
>>> sphere_volume(270)
ERROR
0
Testing the function at edge cases.
>>> sphere_volume(0)
ERROR
0
>>> sphere_volume(2)
33.510321638291124
>>> sphere_volume(99)
4064378.94691403
>>> sphere_volume(101)
ERROR
0
'''
if(radius<1 or radius>100):
print("ERROR")
return 0
else:
return (4/3*math.pi*(radius**3))
try:
print(circle_area(float(input("Enter the radius of circle to find
it's area:"))))
print(sphere_surface_area(float(input("Enter the radius of sphere
to find it's surface area:"))))
print(sphere_volume(float(input("Enter the radius of sphere to find
it's volume:"))))
except ValueError:
print("Please enter a valid value of radius")
if __name__=='__main__':
testmod(verbose = True)