Grocery List App
Maximum Score: 10 pts (see red text for point breakdown)
For this problem, you will be writing some functions to implement an app thatcreates and manages a grocery list.These functions will go in the file named grocery_app.py.
Function: create_database
The create_database function should create and return a database ofgrocery prices.This database will be filled with data from a file the user selects.
The database should be a dictionary from the name (a string value) to theprice (a float value).<#points 0.5 pts>
The file with the grocery price info will have a format of “one item perline.”The name of the item will come first followed by the price of the item.The name and price will be separated by whitespace (e.g. spaces), notcommas.You should be able to use the split() method without any parameters tosplit each line up into the two fields.The following is an example of the file format.
apple $0.50 banana $0.75 potato chips $0.99 soda $1.25 soy milk $4.39
Note that there will be a dollar sign (“$”) sign in the price.However, you should use the string slice operator to get rid of that partof the field before using the float() function to convert the pricestring to a float value.
The name of the grocery database file should be input by the user, using the(aptly named) input() function.I have provided a sample grocery database file named grocery_db.txt thatyou can use to test your program.
Successfully reading all entries from the file is worth 2 points total:
Reads in all items from the file, regardless of how many there are.<#points 1 pt>
Successfully adds each entry to the grocery price database.<#points 1 pt>
Function: print_grocery_list
This function will print all the items in the grocery list, along with theirprices.It should have two parameters, the list of grocery items (a list of strings),and a dictionary that associates grocery items (key) with their cost (value).
For example, if our list contained “apple’, “potato chips’, and “soda’, usingthe prices given in the example database above, this function would print.
1. apple : $0.502. potato chips : $0.993. soda : $1.25Total Cost : $2.74
If the list is empty, this function will print nothing.
This function is worth <#points 2 pts>, broken down as follows:
<#points 1 pt> for correctly printing all items in the given list and having thecorrect printing format.
<#points 1 pt> for correctly summing up the costs of all items in the listand printing the “Total Cost” at the end.
Function: get_max_cost
This function will return the cost of the highest priced item in a grocerylist.It should have two parameters, the list of grocery items (a list of strings),and a dictionary that associates grocery items (key) with their cost (value).If the list is empty, your function should return 0.0.
For this function you may assume that all items in the list are also in thedictionary of costs.
The get_max_cost function is worth 2 total points:
Correct handling of an empty list. <#points 0.5 pts>
Correct handling of a non-empty list. <#points 1 pt>
Function: grocery_app
This function will be the main function for the grocery list application, andshould work as follows.
It should first call the create_database function to create the database ofgrocery prices.<#points 0.5 pts>
Next, it should create an empty grocery list.Eventually you will add strings containing the names of items to this list.
It should then ask the user to enter the names of three items to add to thegrocery list.
You should ask for one item at a time.Use the input function to get input directly from the user.<#points 0.5 pts>
If the item exists in the grocery price database, you should append thisto the end of your grocery list.<#points 0.5 pts>
If the item does NOT exist in the database, you should print out amessage to let the user know that the item doesn’t exist.<#points 0.5 pts>
To determine whether an item is in the database, you can use the “in”operator to determine if the item name is in the list dictionary’s keys.
You should only stop asking the user once they have entered three validitems.For this, you will need to use a while loop.<#points 1 pt>
Print out the contents of your grocery list using your print_grocery_listfunction.<#points 0.5 pts>
Next, you should test that your get_max_value function works by printing outits result.Let the user know what you are doing by printing out a message like:”The highest priced item on your list costs: $1.99.”<#points 0.5 pts>
Extra Credit: Print a friendly message thanking the user for using yourgrocery list app. <#points 0 pts>
Testing Your Grocery App
You should be able to test your full grocery app by running it from the VS Code terminal.
python3 grocery_app.py
However, it is strongly recommended that you test every function you writeindividually in the Python REPL before moving on to the next function.
Example Run of Grocery App
The following is an example of what should be output when running yourgrocery_app.py file.
Welcome to the Grocery List Organizer!Enter the name of the file with price information: grocery_db.txtCreated price database with 10 entries.You will now enter 3 items to add to your list.Enter the item name: bananaEnter the item name: rainbowsI’m sorry, rainbows is not in the database. Please try again.Enter the item name: blueberry muffinEnter the item name: tofuHere is your grocery list:1. banana : $0.752. blueberry muffin : $1.493. tofu : $2.39Total Cost : $4.63The highest priced item on your list costs: $2.39.Thank you for using the Grocery List Organizer!Be sure to rate us on the COMP110 App store.
Commenting Your Code
You are expected to follow best practices in commenting your code.This includes:
Updating the comment at the top ofgrocery_app.py with your and your partner’s info.
Including a docstring comment at the beginning of every function you writethat succinctly describes what it does.
Add comments to “tricky” spots in your code to explain to yourself, yourpartner, and the grader what the code is doing.
Using good variable names to make code more readable by others.
Commenting is worth 2 pts so please take it seriously.
