Description
With gas prices constantly fluctuating and consumers being more conscientious of environmental concerns, fuel economy is becoming a higher priority for many. Nevertheless, the vast majority of drivers don’t know how to accurately measure their vehicle’s actual fuel economy, which most of us in the United States measure in miles per gallon. But with a little bit of programming help, it’s easy to calculate and track gas mileage over time.
Assignment
Your program will gather and record information about a vehicle’s mileage and gas consumption, and then use this information to calculate the vehicle’s fuel economy in miles per gallon. Your program will feature a very simple menu that allows the user to perform any one of four operations, one at a time, repeatedly until the user chooses to quit the program. When prompted, the user may select one of the four operations by inputting a unique letter corresponding to one of the operations. The four operations (and their unique letters) are:
[r] Record Gas Consumption: This operation allows the user to record gas consumption when they fill their vehicle with gas. It asks the user for three pieces of information: the date, the number of miles traveled since the vehicle was last filled with gas, and the number of gallons of gas that was just added to the vehicle. You should store each of these values into a dictionary that contains three keys (one key for each of the three values), and then save the dictionary by appending it to a list.
[l] List Mileage History: This operation does not ask the user for any additional information. Your program will print a list of all recorded gas consumption entries, one per line. Each line should contain the date, the number of miles traveled, and the number of gallons added. Also print the gas mileage (miles per gallon) for this entry, calculated using the number of miles traveled and the number of gallons added (from this entry only).
[c] Calculate Gas Mileage: This operation does not ask the user for any additional information. Your program will calculate the average gas mileage from all recorded gas consumption entries and print the average gas mileage on a single line. If no gas consumption entries have been recorded, print a message that asks the user to first record their gas consumption.
[q] Quit: When this operation is selected, your program will print a friendly goodbye message to the user and then terminate.
Something else? If the user enters a different letter or phrase, print a message informing the user that this option is invalid, and allow them to try again.
Extra Challenges
Add a bit of friendliness to your program! When your program first starts, before the menu options are first printed, display a brief message explaining your program and how to use it.
Because this assignment does not require you to save the gas consumption records to a file, you may assume that all records will be lost when the program quits. However, wouldn’t it be useful if the records were not lost? Consider upgrading your program to write all records to a file just before the program quits, and then also load any records from the same file when the program is started again.
Hints
Before starting, practice using dictionaries and lists. You’ll need to know how to create a dictionary, set a value by key, retrieve a value by key, append a dictionary to a list, and iterate over a list of dictionaries.
In order to calculate gas mileage, you should store the number of miles traveled and the number of gallons added as float values. Here is a simple way to convert a string or integer value to a float value: gallons = float(gallons)
Sample
Program execution:
What would you like to do?
[r] Record Gas Consumption
[l] List Mileage History
[c] Calculate Gas Mileage
[q] Quit
Enter an option: c
You first need to record your gas consumption!
What would you like to do?
[r] Record Gas Consumption
[l] List Mileage History
[c] Calculate Gas Mileage
[q] Quit
Enter an option: r
What is the date? 1/15/16
How many miles did you drive since last filling up? 300
How many gallons of gas did you add to your tank? 10
Saved!
What would you like to do?
[r] Record Gas Consumption
[l] List Mileage History
[c] Calculate Gas Mileage
[q] Quit
Enter an option: r
What is the date? 1/25/16
How many miles did you drive since last filling up? 200
How many gallons of gas did you add to your tank? 8
Saved!
What would you like to do?
[r] Record Gas Consumption
[l] List Mileage History
[c] Calculate Gas Mileage
[q] Quit
Enter an option: l
On 1/15/16: 300.0 miles traveled using 10.0 gallons. Gas mileage: 30.0 MPG
On 1/25/16: 200.0 miles traveled using 8.0 gallons. Gas mileage: 25.0 MPG
What would you like to do?
[r] Record Gas Consumption
[l] List Mileage History
[c] Calculate Gas Mileage
[q] Quit
Enter an option: c
Average gas mileage: 27.7777777778 MPG
What would you like to do?
[r] Record Gas Consumption
[l] List Mileage History
[c] Calculate Gas Mileage
[q] Quit
Enter an option: x
Sorry, that option is invalid.
What would you like to do?
[r] Record Gas Consumption
[l] List Mileage History
[c] Calculate Gas Mileage
[q] Quit
Enter an option: q
Bye! See you next time!