a = 5
b = 10
def sum():
return a + b
def multiply():
return a * b
def main():
print(sum())
print(multiply())
main()
15
50
Write two functions: sphere_area
and sphere_volume
that calculates the area and the volume of a sphere:
use 3.1415
for \(\pi\) and return the rounded value.
\[ a = 4 \cdot \pi \cdot radius^2 \]
\[
v = {4 / 3} \cdot \pi \cdot radius^3
\] Name your file as sphere.py
and submit to gradescope.
Test case:
def sphere_area(radius):
"calculates the area of a sphere of given radius"
area = 4 * 3.1415 * radius**2
return round(area, 2)
def sphere_volume(radius):
"calculates the volume of a sphere of given radius"
volume = (4 / 3) * 3.1415 * radius**3
return round(volume, 2)
def main():
r = .75
v = sphere_volume(r)
a = sphere_area(r)
print(v, a)
main()
1.77 7.07
Comparing two formulas: \[ a = 4 \cdot \pi \cdot radius^2 \]
\[ v = {4 / 3} \cdot \pi \cdot radius^3 \] We can use area when calculating volume: \[ v = {1 / 3} \cdot a \cdot radius \]
Modify your sphere_volume
function by calling sphere_area
inside the function.
def sphere_area(radius):
"calculates the area of a sphere of given radius"
area = 4 * 3.1415 * radius**2
return round(area, 2)
def sphere_volume(radius):
"calculates the volume of a sphere of given radius"
volume = (1 / 3) * sphere_area(radius) * radius
return round(volume, 2)
def main():
r = .75
v = sphere_volume(r)
a = sphere_area(r)
print(v, a)
main()
1.77 7.07
#
for other comments)'''
Xinchen Yu
CSC110
Class Demonstration
This program has two functions: one to calculate the area of a sphere,
the other to calculate the volume of a sphere.
The main() function is called to print to the standard output the
area and volume of a sphere of radius .75
'''
def sphere_area(radius):
'''
This function calculates the area of a sphere of given radius.
Args:
radius: integer representing the radius of the sphere
Returns:
The float representing the area of a sphere of the given radius
'''
area = 4 * 3.1415 * radius**2
return round(area, 2)
def sphere_volume(radius):
'''
This function calculates the volume of a sphere of given radius.
Args:
radius: integer representing the radius of the sphere
Returns:
The float representing the volume of a sphere of the given radius
'''
volume = (1 / 3) * sphere_area(radius) * radius
return round(volume, 2)
def main():
'''
This function prints the area and volume of a sphere of radius .75.
Args:
None
Returns:
None
'''
r = .75
a = sphere_area(r)
v = sphere_volume(r)
print(a, v)
main()
7.07 1.77
Write a function that does the following:
volume
radius
and height
radius
and height
. Volume is area multiplied by height.