ploblem solving assignment in python 3
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 that creates and manages a grocery list. These functions will go in the file named
create_databasefunction should create and return a database of grocery prices. This database will be filled with data from a file the user selects.
- The database should be a dictionary from the name (a
stringvalue) to the price (a
floatvalue). <#points 0.5 pts>
- The file with the grocery price info will have a format of “one item per line.” 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), not commas. You should be able to use the
split()method without any parameters to split each line up into the two fields.The following is an example of the file format.
apple $0.50_x000D_ banana $0.75_x000D_ potato chips $0.99_x000D_ soda $1.25_x000D_ soy milk $4.39_x000D_
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 part of the field before using the
float()function to convert the price string to a
- 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.txtthat you 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>
This function will print all the items in the grocery list, along with their prices. 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’, using the prices given in the example database above, this function would print.
1. apple : $0.50_x000D_ 2. potato chips : $0.99_x000D_ 3. soda : $1.25_x000D_ _x000D_ Total Cost : $2.74_x000D_
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 the correct printing format.
- <#points 1 pt> for correctly summing up the costs of all items in the list and printing the “Total Cost” at the end.
This function will return the cost of the highest priced item in a grocery list. 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 the dictionary of costs.
get_max_costfunction 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>
This function will be the main function for the grocery list application, and should work as follows.
- It should first call the
create_databasefunction to create the database of grocery 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 the grocery list.
- You should ask for one item at a time. Use the
inputfunction to get input directly from the user. <#points 0.5 pts>
- If the item exists in the grocery price database, you should append this to the end of your grocery list. <#points 0.5 pts>
- If the item does NOT exist in the database, you should print out a message 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 valid items. For this, you will need to use a
whileloop. <#points 1 pt>
- You should ask for one item at a time. Use the
- 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_valuefunction works by printing out its 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 your grocery 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.
However, it is strongly recommended that you test every function you write individually 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 your
Welcome to the Grocery List Organizer!_x000D_ _x000D_ Enter the name of the file with price information: grocery_db.txt_x000D_ _x000D_ Created price database with 10 entries._x000D_ _x000D_ You will now enter 3 items to add to your list._x000D_ _x000D_ Enter the item name: banana_x000D_ _x000D_ Enter the item name: rainbows_x000D_ _x000D_ I'm sorry, rainbows is not in the database. Please try again._x000D_ _x000D_ Enter the item name: blueberry muffin_x000D_ _x000D_ Enter the item name: tofu_x000D_ _x000D_ Here is your grocery list:_x000D_ _x000D_ 1. banana : $0.75_x000D_ 2. blueberry muffin : $1.49_x000D_ 3. tofu : $2.39_x000D_ _x000D_ Total Cost : $4.63_x000D_ _x000D_ The highest priced item on your list costs: $2.39._x000D_ _x000D_ Thank you for using the Grocery List Organizer!_x000D_ Be sure to rate us on the COMP110 App store._x000D_
Commenting Your Code
You are expected to follow best practices in commenting your code. This includes:
- Updating the comment at the top of
grocery_app.pywith your and your partner’s info.
- Including a docstring comment at the beginning of every function you write that succinctly describes what it does.
- Add comments to “tricky” spots in your code to explain to yourself, your partner, 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.