This function definition has many parts:
two is the name of the function() is the parameter list (Here, it is empty)return 2 is a statement that causes the function to cease and produce the value 2add_one is the name of the function(n) is the parameter listreturn n + 1 is a statement that causes the function to cease and produce the value n + 1def cook_food(order):
message = "Your " + order + " is ready!"
return message
meal = cook_food("pasta")
print(meal)
meal = cook_food("burger")
print(meal)Your pasta is ready!
Your burger is ready!
cook_food prepares a meal.meal variable.print function displays that message to the caller.Remember this from the last set of slides?
Function name is calculate_area. Given a radius parameter, it returns the rounded area of the circle.
Write a function that calculates the volume of a sphere:
sphere_volumeradius3.1415 for \(\pi\)):\[ v = {4 / 3} \cdot \pi \cdot radius^3 \]
sphere_volume(.75) should return 1.77.Write a function that calculates the area of a sphere:
sphere_arearadius3.1415 for \(\pi\)):\[ a = 4 \cdot \pi \cdot radius^2 \]
sphere_area(.75) should return 7.07.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
Write a Python function named hypotenuse that takes two arguments: a and b representing the length of the two non-hypotenuse sides of a right triangle. The function calculate the hypotenuse according to the Pythagorean theorem: \(c = \sqrt(a^2 + b^2)\). Return it rounded at two decimals.
Test cases: hypotenuse(3, 4) should return 5.0, hypotenuse(10, 10) should return 14.14
Name your file hypotenuse.py and submit to gradescope.
def sqrt(n):
'''
This function calculates the square root of a number
Args:
n: integer or float
Returns:
The square root of n
'''
return n**0.5
def hypotenuse(a, b):
'''
This function calculates the hypotenuse of a right angle triangle.
Args:
a: number (integer or float) representing one of the non-hypotenuse sides
b: number (integer or float) representing one of the non-hypotenuse sides
Returns:
Float representing the length of the hypotenuse given a and b
'''
h = sqrt(a**2 + b**2)
return round(h, 2)
def main():
'''
This function calls the hypotenuse function to calculate and then
print out the hypotenuse of a right angle triangle of sides 3 and 4
and the hypotenuse of a right angle triange of sides 10 and 10
'''
result = hypotenuse(3, 4)
print(result)
result = hypotenuse(10, 10)
print(result)
main()5.0
14.14