Any topic (writer’s choice)
May 15, 2020
Police Leadership
May 15, 2020
Show all

Dictionaries

You should create a program that manages a tab-separated text file containing employees travel expenses. There are 4 and only 4 employees who travel for the company: alice, bob, carl, and diane. Each record in the data file will contain an employee name, a destination city, miles traveled, and gallons used.

In the data file itself, each field in a record is separated by a tab character (“t”). Here are the records to be used in the file; it may not look like it, but each field is tab-separated. If you copy and paste this data into your data file, you may or may not need to edit it a bit in Notepad (or similar) to make sure there is only 1 tab between each of the 4 fields. There is no tab between st. and louis, only a space. When this data is read into your program it will be converted into a list of dictionaries, where each row in the data is a dictionary with 4 keys: name, city, miles, and gallons.

alice    chicago    663    20.7
bob    indianapolis    226    8.7
alice    nashville    409    16.4
carl    indianapolis    243    8.4
diane    st. louis    581    16.4
bob    st. louis    560    18.1
bob    cincinnati    237    6.6
alice    chicago    681    17.9
alice    nashville    422    14.6
diane    chicago    676    22.5
carl    indianapolis    243    6.4
diane    indianapolis    276    7.7
diane    indianapolis    241    9.4
You will likely find it easier to use the data file as given in this link: travels.txt

You will find the base code for this program given near the bottom of the page in the lecture notes that discuss dictionaries, along with a video explaining how it works. That code will already contain functions for creating a menu, reading the data from a file, adding a record, and a function to store the data back to the text tile–all using the data given above! That discussion will certainly help you get a good start on this assignment. A picture of the menu you need to use is given below. Note option 2, which is new.

menu

Here’s what it might look like if you select option 1 to display all the data, with the data formatted to line up in neat columns. As mentioned above, code for this much of the program (with the exception of option 2) can be copied from the lecture notes.

everyone’s data

So, what is it that you need to do? You need to add the functionality so that clicking option 2 will display the total miles and gallons used by one of the employees. When option 2 is exercised you should enter one of the 4 employee’s first names, then the following should be displayed: that person’s name and total miles traveled, for all cities, total gallons used, average miles per gallon overall, and the expense value for the total miles at 75 cents per mile. Like this, for alice.

We exercise option 2, then enter alice’s name, then we see that alice has racked up a total of 2175 miles, used a total of 69.6 gallons for an average miles-per-gallon value of 31.3 mpg. And, alice should get an expense check for 2175 * .75 = $ 1631.25. If using option 2 you entered the name bob, you’d see bob’s calculated data (1023 total mile with the given data, etc.).

alice’s data

So, basically, you need to add 1 function to the base code given in the notes. For option 2, you will essentially need to read each record with a loop and accumulate totals for miles and gallons, but only if the entered user name matches a name in a record (think if-statement). If you’ve forgotten what an accumulator is, go back to the material where the for-loop was introduce.

So, enter a name, set up a for-loop that loops thru the data, if the entered name matched the name in the record, add the miles to a variable, the gallons to a variable, with the loop keeping a running total.

After the loop runs, mpg is total miles divided by total gallons, and the expense check is just total miles times .75.

For full credit:

Your program should properly read and store the given data to and from the tab-separated data file. That is, I should be able to use your program with my data file without error.
When the data is read into your program it should be organized as a list of dictionaries, where each dictionary is a row in the file, using these dictionary keys: name, city, miles, gallons.
You need to add functionality to display the user’s name, total miles traveled, total gallons used, average mpg, and the expense of the total miles at 75 cents per mile. This should work for any of the 4 proper user names you enter.

Leave a Reply

Your email address will not be published. Required fields are marked *