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_feetthat takes a parametermilesand returns the number of feet inmilesmiles. 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_secondsthat takes three parametershours,minutesandsecondsand returns the total number of seconds forhourshours,minutesminutes andsecondsseconds. 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_perimeterthat takes two parameterswidthandheightcorresponding 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_areathat takes two parameterswidthandheightcorresponding 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_circumferencethat takes a single parameterradiuscorresponding to the radius of a circle in inches and returns the the circumference of a circle with radiusradiusin inches. Do not use $\pi = 3.14$, instead use themathmodule 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_areathat takes a single parameterradiuscorresponding to the radius of a circle in inches and returns the the area of a circle with radiusradiusin square inches. Do not use $\pi = 3.14$, instead use themathmodule 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_valuethat takes three parameterspresent_value,annual_rateandyearsand returns the future value ofpresent_valuedollars invested atannual_ratepercent interest, compounded annually foryearsyears. Future value template — Future value solution — Future value (Checker)Write a Python function
name_tagthat takes as input the parametersfirst_nameandlast_name(strings) and returns a string of the form"My name is % %."where the percents are the stringsfirst_nameandlast_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_agethat 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 ofnameandage. 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_distancethat takes as the parametersx0,y0,x1andy1, 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_areathat 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_distanceas 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_digitsthat takes an integernumberin 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
powerballthat 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 therandommodule and the functionrandom.randrangeto 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()