03 Checkpoint: Writing Functions

Purpose

Check your understanding of writing your own functions with parameters and then calling those functions with arguments.

Problem Statement

Many vehicle owners record the fuel efficiency of their vehicles as a way to track the health of the vehicle. If the fuel efficiency of a vehicle suddenly drops, there is probably something wrong with the engine or drive train of the vehicle. In the United States, fuel efficiency for gasoline powered vehicles is calculated as miles per gallon. In most other countries, fuel efficiency is calculated as liters per 100 kilometers.

The formula for computing fuel efficiency in miles per gallon is the following:

mpg =
endstart
gallons

where start and end are both odometer values in miles and gallons is a fuel amount in U.S. gallons.

The formula for converting miles per gallon to liters per 100 kilometers is the following:

lp100k =
235.215
mpg

Assignment

Write a Python program that asks the user for three numbers:

  1. A starting odometer value in miles
  2. An ending odometer value in miles
  3. An amount of fuel in gallons

Your program must calculate and print fuel efficiency in both miles per gallon and liters per 100 kilometers. Your program must have three functions named as follows:

  1. main
  2. miles_per_gallon
  3. lp100k_from_mpg

All user input and printing must be in the main function. In other words, the miles_per_gallon and lp100k_from_mpg functions must not call the the input or print functions.

Helpful Documentation

Steps

Copy and paste the following code into a new program named fuel_usage.py. Use the pasted code as a design as you write your program. Write code for each of the three functions.

def main():
    # Get an odometer value in U.S. miles from the user.

    # Get another odometer value in U.S. miles from the user.

    # Get a fuel amount in U.S. gallons from the user.

    # Call the miles_per_gallon function and store
    # the result in a variable named mpg.

    # Call the lp100k_from_mpg function to convert the
    # miles per gallon to liters per 100 kilometers and
    # store the result in a variable named lp100k.

    # Display the results for the user to see.
    pass


def miles_per_gallon(start_miles, end_miles, amount_gallons):
    """Compute and return the average number of miles
    that a vehicle traveled per gallon of fuel.

    Parameters
        start_miles: An odometer value in miles.
        end_miles: Another odometer value in miles.
        amount_gallons: A fuel amount in U.S. gallons.
    Return: Fuel efficiency in miles per gallon.
    """
    return


def lp100k_from_mpg(mpg):
    """Convert miles per gallon to liters per 100
    kilometers and return the converted value.

    Parameter mpg: A value in miles per gallon
    Return: The converted value in liters per 100km.
    """
    return


# Call the main function so that
# this program will start executing.
main()

Testing Procedure

Verify that your program works correctly by following each step in this testing procedure:

  1. Run your program and enter the inputs shown below. Ensure that your program’s output matches the output below.
    > python fuel_usage.py
    Enter the first odometer reading (miles): 30462
    Enter the second odometer reading (miles): 30810
    Enter the amount of fuel used (gallons): 11.2
    31.1 miles per gallon
    7.57 liters per 100 kilometers

Sample Solution

When your program is finished, view the sample solution for this assignment to compare your solution to that one. Before looking at the sample solution, you should work to complete this checkpoint program. However, if you have worked on it for at least an hour and are still having problems, feel free to use the sample solution to help you finish your program.

Call Graph

The following call graph shows the function calls and returns in the sample solution for this assignment. From this call graph we see that the computer starts executing the sample program by calling the main function. While executing the main function, the computer calls the input and float functions. Then the computer calls the miles_per_gallon and lp100k_from_mpg functions. Finally the computer calls the print function which is the end of the program.

A function call graph for this assignment

Ponder

After you finish this assignment, congratulate yourself because you wrote a Python program with three user-defined functions named main, miles_per_gallon, and lp100k_from_mpg. Is it important that you know how to write your own functions? Why?

Submission

When complete, report your progress in the associated I‑Learn quiz.