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.

  1. Write a Python function miles_to_feet that takes a parameter miles and returns the number of feet in miles miles. Miles to feet templateMiles to feet solutionMiles 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)
    
  2. Write a Python function total_seconds that takes three parameters hours, minutes and seconds and returns the total number of seconds for hours hours, minutes minutes and seconds seconds. Hours to seconds templateHours to seconds solutionHours 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)
    
  3. Write a Python function rectangle_perimeter that takes two parameters width and height corresponding to the lengths of the sides of a rectangle and returns the perimeter of the rectangle in inches. Perimeter of rectangle templatePerimeter of rectangle solutionPerimeter of rectangle (Checker)

  4. Write a Python function rectangle_area that takes two parameters width and height corresponding to the lengths of the sides of a rectangle and returns the area of the rectangle in square inches. Area of rectangle templateArea of rectangle solutionArea of rectangle (Checker)

  5. Write a Python function circle_circumference that takes a single parameter radius corresponding to the radius of a circle in inches and returns the the circumference of a circle with radius radius in inches. Do not use $\pi = 3.14$, instead use the math module to supply a higher-precision approximation to $\pi$. Circumference of circle templateCircumference of circle solutionCircumference of circle (Checker)

  6. Write a Python function circle_area that takes a single parameter radius corresponding to the radius of a circle in inches and returns the the area of a circle with radius radius in square inches. Do not use $\pi = 3.14$, instead use the math module to supply a higher-precision approximation to $\pi$. Area of circle templateArea of circle solutionArea of circle (Checker)

  7. Write a Python function future_value that takes three parameters present_value, annual_rate and years and returns the future value of present_value dollars invested at annual_rate percent interest, compounded annually for years years. Future value templateFuture value solutionFuture value (Checker)

  8. Write a Python function name_tag that takes as input the parameters first_name and last_name (strings) and returns a string of the form "My name is % %." where the percents are the strings first_name and last_name. Reference the test cases in the provided template for an exact description of the format of the returned string. Name tag templateName tag solutionName tag (Checker)

  9. Write a Python function name_and_age that takes as input the parameters name (a string) and age (a number) and returns a string of the form "% is % years old." where the percents are the string forms of name and age. Reference the test cases in the provided template for an exact description of the format of the returned string. Name and age templateName and age solutionName 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)
    
  10. Write a Python function point_distance that takes as the parameters x0, y0, x1 and y1, and returns the distance between the points $(x0,y0)$ and $(x1, y1)$. Point distance templatePoint distance solutionPoint 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)
    
  11. Challenge: Write a Python function triangle_area that takes the parameters x0, y0, x1,y1, x2, and y2, and returns the area of the triangle with vertices $(x0,y0)$, $(x1, y1)$ and $(x2, y2)$. (Hint: use the function point_distance as a helper function and apply Heron’s formula .) Triangle area templateTriangle area solutionTriangle 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)
    
  12. Challenge: Write a Python function print_digits that takes an integer number 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 templatePrint digits solutionPrint 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)
    
  13. 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 the random module and the function random.randrange to generate the appropriate random numbers.Note that this function should print the desired message, rather than returning it as a string. Powerball templatePowerball solutionPowerball (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()