Practice Quiz: Functions

Practice Quiz: Functions

6. This function converts miles to kilometers (km).

Complete the code to return the result of the conversion.

NOTE: The following items occur outside of the function. Do not try to change the indentations on the associated code or you will receive an error.

Call the function to convert the trip distance from miles to kilometers.

Fill in the blank to print the result of the conversion.

Calculate the round-trip in kilometers by doubling the result, and fill in the blank to print the result.

# 1) Complete the function to return the result of the conversion
def convert_distance(miles):
km = miles * 1.6 # approximately 1.6 km in 1 mile
return ___

# Do not indent any of the following lines of code as they are
# meant to be located outside of the function above

my_trip_miles = 55

# 2) Convert my_trip_miles to kilometers by calling the function above
my_trip_km = ___(my_trip_miles)

# 3) Fill in the blank to print the result of the my_trip_km conversion
print("The distance in kilometers is " + str(___))

# 4) Calculate the round-trip in kilometers by doubling the result of
# my_trip_km. Fill in the blank to print the result.
print("The round-trip in kilometers is " + str(___*___))

  • # 1) Complete the function to return the result of the conversion
    def convert_distance(miles):
    km = miles * 1.6 # approximately 1.6 km in 1 mile
    return km

    # Do not indent any of the following lines of code as they are
    # meant to be located outside of the function above

    my_trip_miles = 55


    # 2) Convert my_trip_miles to kilometers by calling the function above
    my_trip_km = convert_distance(my_trip_miles)


    # 3) Fill in the blank to print the result of the my_trip_km conversion
    print(“The distance in kilometers is ” + str(my_trip_km))


    # 4) Calculate the round-trip in kilometers by doubling the result of
    # my_trip_km. Fill in the blank to print the result.
    print(“The round-trip in kilometers is ” + str(my_trip_km*2))

7. This function compares two numbers and returns them in increasing order.

Fill in the blanks, so the print statement displays the result of the function call in order.

Hint: if a function returns multiple values, don't forget to store these values in multiple variables
# This function compares two numbers and returns them
# in increasing order.
def order_numbers(number1, number2):
if number2 > number1:
return number1, number2
else:
return number2, number1

# 1) Fill in the blanks so the print statement displays the result
# of the function call
___, ___ = order_numbers(100, 99)
print(smaller, bigger)

  • smaller, bigger

8. What are the values passed into functions as input called?

  • Variables
  • Return values
  • Parameters
  • Data types

9. Complete the first line of the “print_seconds” function so that it accepts three parameters: hours, minutes, and seconds. Remember to use the “def” keyword to tell the Python interpreter the block of code is intended to define a function.
___ ___(___, ___, ___):
print(hours*3600+minutes*60+seconds)


print_seconds(1,2,3)

  • def print_seconds(hours, minutes, seconds):
    print(hours*3600+minutes*60+seconds)
    print_seconds(1, 2, 3)

10. What is the purpose of the def keyword?

  • Used to define a new function
  • Used to define a return value
  • Used to define a new variable
  • Used to define a new parameter

Devendra Kumar

Project Management Apprentice at Google

Leave a Reply