Practice Exercises for Functions#
Solve each of the practice exercises below. Each problem includes three CodeSkulptor links: one for a template that you should use as a starting point for your solution, one to our solution to the exercise, and one to a tool that automatically checks your solution.
Write a Python function
miles_to_feet
that takes a parametermiles
and returns the number of feet inmiles
miles. Miles to feet template — Miles to feet solution — Miles to feet (Checker)def miles_to_feet(miles): return miles * 5280 def test(miles): print(f'{miles} miles equals {miles_to_feet(miles)} feet.') test(13) test(57) test(82.67)
Write a Python function
total_seconds
that takes three parametershours
,minutes
andseconds
and returns the total number of seconds forhours
hours,minutes
minutes andseconds
seconds. Hours to seconds template — Hours to seconds solution — Hours to seconds (Checker)def total_seconds(hours, minutes, seconds): return (hours * 60 + minutes) * 60 + seconds def test(hours, minutes, seconds): print(f'{hours} hours, {minutes} minutes, and {seconds} seconds totals to', f'{total_seconds(hours, minutes, seconds)} seconds') test(7, 21, 37) test(10, 1, 7) test(1, 0, 1)
Write a Python function
rectangle_perimeter
that takes two parameterswidth
andheight
corresponding to the lengths of the sides of a rectangle and returns the perimeter of the rectangle in inches. Perimeter of rectangle template — Perimeter of rectangle solution — Perimeter of rectangle (Checker)Write a Python function
rectangle_area
that takes two parameterswidth
andheight
corresponding to the lengths of the sides of a rectangle and returns the area of the rectangle in square inches. Area of rectangle template — Area of rectangle solution — Area of rectangle (Checker)Write a Python function
circle_circumference
that takes a single parameterradius
corresponding to the radius of a circle in inches and returns the the circumference of a circle with radiusradius
in inches. Do not use $\pi = 3.14$, instead use themath
module to supply a higher-precision approximation to $\pi$. Circumference of circle template — Circumference of circle solution — Circumference of circle (Checker)Write a Python function
circle_area
that takes a single parameterradius
corresponding to the radius of a circle in inches and returns the the area of a circle with radiusradius
in square inches. Do not use $\pi = 3.14$, instead use themath
module to supply a higher-precision approximation to $\pi$. Area of circle template — Area of circle solution — Area of circle (Checker)Write a Python function
future_value
that takes three parameterspresent_value
,annual_rate
andyears
and returns the future value ofpresent_value
dollars invested atannual_rate
percent interest, compounded annually foryears
years. Future value template — Future value solution — Future value (Checker)Write a Python function
name_tag
that takes as input the parametersfirst_name
andlast_name
(strings) and returns a string of the form"My name is % %."
where the percents are the stringsfirst_name
andlast_name
. Reference the test cases in the provided template for an exact description of the format of the returned string. Name tag template — Name tag solution — Name tag (Checker)Write a Python function
name_and_age
that takes as input the parametersname
(a string) andage
(a number) and returns a string of the form"% is % years old."
where the percents are the string forms ofname
andage
. Reference the test cases in the provided template for an exact description of the format of the returned string. Name and age template — Name and age solution — Name and age (Checker)def name_and_age(name, age): return f'{name} is {age} years old' def test(name, age): print(name_and_age(name, age)) test("Joe Warren", 52) test("Scott Rixner", 40) test("John Greiner", 46)
Write a Python function
point_distance
that takes as the parametersx0
,y0
,x1
andy1
, and returns the distance between the points $(x0,y0)$ and $(x1, y1)$. Point distance template — Point distance solution — Point distance (Checker)def point_distance(x0, y0, x1, y1): return ( (x0 - y0)**2 + (x1 - y1)**2 )**.5 def test(x0, y0, x1, y1): print( "The distance from (" + str(x0) + ", " + str(y0) + ") to", end=' ') print( "(" + str(x1) + ", " + str(y1) + ") is", end=' ') print( str(point_distance(x0, y0, x1, y1)) + ".") test(2, 2, 5, 6) test(1, 1, 2, 2) test(0, 0, 3, 4)
Challenge: Write a Python function
triangle_area
that takes the parametersx0
,y0
,x1
,y1
,x2
, andy2
, and returns the area of the triangle with vertices $(x0,y0)$, $(x1, y1)$ and $(x2, y2)$. (Hint: use the functionpoint_distance
as a helper function and apply Heron’s formula .) Triangle area template — Triangle area solution — Triangle area (Checker)def triangle_area(x0, y0, x1, y1, x2, y2): a = ( (x0 - x1)**2 + (y0 - y1)**2 )**.5 b = ( (x1 - x2)**2 + (y1 - y2)**2 )**.5 c = ( (x2 - x0)**2 + (y2 - y0)**2 )**.5 s = (a + b + c)/2 area = (s * (s-a) * (s-b) * (s-c))**.5 return area def test(x0, y0, x1, y1, x2, y2): print( "A triangle with vertices (" + str(x0) + "," + str(y0) + "),", end=' ') print( "(" + str(x1) + "," + str(y1) + "), and", end=' ') print( "(" + str(x2) + "," + str(y2) + ") has an area of", end=' ') print( str(triangle_area(x0, y0, x1, y1, x2, y2)) + ".") test(0, 0, 3, 4, 1, 1) test(-2, 4, 1, 6, 2, 1) test(10, 0, 0, 0, 0, 10)
Challenge: Write a Python function
print_digits
that takes an integernumber
in the range $[0,100)$, i.e., at least 0, but less than 100. It prints the message"The tens digit is %, and the ones digit is %."
, where the percent signs should be replaced with the appropriate values. (Hint: Use the arithmetic operators for integer division//
and remainder%
to find the two digits. Note that this function should print the desired message, rather than returning it as a string. Print digits template — Print digits solution — Print digits (Checker)def print_digits(n): tens = n // 10 % 10 ones = n % 10 print(f'The tens digit is {tens}, and the ones digit is {ones}.') print_digits(42) print_digits(99) print_digits(5)
Challenge: Powerball is lottery game in which 6 numbers are drawn at random. Players can purchase a lottery ticket with a specific number combination and, if the number on the ticket matches the numbers generated in a random drawing, the player wins a massive jackpot. Write a Python function
powerball
that takes no arguments and prints the message"Today's numbers are %, %, %, %, and %. The Powerball number is %."
. The first five numbers should be random integers in the range $[1,60)$, i.e., at least 1, but less than 60. In reality, these five numbers must all be distinct, but for this problem, we will allow duplicates. The Powerball number is a random integer in the range $[1,36)$, i.e., at least 1 but less than 36. Use therandom
module and the functionrandom.randrange
to generate the appropriate random numbers.Note that this function should print the desired message, rather than returning it as a string. Powerball template — Powerball solution — Powerball (Checker)def powerball(): import random def r(): return random.randrange(1, 36) print( f"Today's numbers are {r()}, {r()}, {r()}, {r()}, and {r()}.", f" The Powerball number is {r()}." ) powerball() powerball() powerball()