W05 Project: Grocery Store
Purpose
Prove that you can write a Python program that reads CSV files and creates, populates, and uses a dictionary.
Project
Do not use AI to generate the code for this program.
Using AI to generate this program is a violation of the course AI policy and may result in receiving a 0 on the assignment, failing the course, or being removed from the program.
If you need help on the assignment or have questions about AI use, please ask your instructor.
Background
Your uncle has a grocery store, he has just started to use an online service that enables his customers to order groceries online. After a customer completes an order, the online service sends a CSV file that contains the customer’s requests to the grocery store. Your uncle has asked you to write a program that reads the CSV file and prints to the terminal window a receipt that lists the purchased items and shows the subtotal, the sales tax amount, and the total.
User Requirements
The program must read two csv files, the customer's order and a product catalog. Each item in the customer's order will be looked up in the product catalog to get get the current price. An order will be displayed in the terminal that shows the customer's order details. Use the following details to create the program.
- Read the products inventory from the file
products.csv. - Read the customer's order from the file
request.csv - For each item in the order, look up the product in the catalog. Use the catalog information to calculate and display the order.
- Display the order receipt.
- Print a store name (you choose the name) at the top of the receipt.
- Print the list of ordered items. Include the item name, quantity ordered and price per item.
- Sum and print the number of ordered items.
- Sum and print the subtotal due.
- Compute and print the sales tax amount. Use 6% as the sales tax rate.
- Compute and print the total amount due.
- Print a thank you message.
- Get the current date and time from your computer’s operating system and print the current date and time.
- Include a
tryblock andexceptblocks to handleFileNotFoundError,PermissionError, andKeyError.
Design
The bulk of the work for this program is performed in the main function. Make sure
you include error handling as described in the user requirements.
Function Specifications
| Function Name | ParametersReturn Type | Description |
|---|---|---|
| read_dictionary | Parametersfilename,key_column_index Return TpeDictionary | This function reads the product data from the csv file passed to the function in the filename parameter. The dictionary key is contained in the csv data column indicated by the key_column_index parameter, the value of each dictionary item is the list derived from the values in the row of the csv file. Function returns a dictionary of products. |
| main | Reads the receipt.csv file, processes the file and displays the receipt according to the user requirements. |
Helpful information.
-
The Reading Files article explains how to setup VS Code so that your Python program can read from a text file.
-
The preparation content for this lesson shows how to read the contents of a CSV file into a compound dictionary and how to read and process a CSV file without storing it in a dictionary.
-
The preparation content for week 4 explains how to find an item in a dictionary.
-
The video titled How to Read and Use a Dictionary (34 minutes) shows a BYU-Idaho faculty member solving a problem that is similar to this prove assignment.
Milestone
Start your project by writing and testing the read_dictionary function.
- Create a folder for this week's project, name it whatever you want.
- Open the folder you just created in VSCode.
- Download both of these files:
products.csvandrequest.csvand save them in your project's folder. - Open the
products.csvfile in VS Code and examine it. Notice that each row in this file contains three values separated by commas: a product number, a product name, and a retail price. Also, notice that each product number in theproducts.csvfile is unique. This means that your program can read theproducts.csvfile into a dictionary and use the product numbers as keys in the dictionary. - Create a file named
receipt.pyin your project folder. - Code the
read_dictionaryfunction. It should open a CSV file for reading and use acsv.readerto read each row and populate a compound dictionary with the contents of theproducts.csvfile. - Create the
mainfunction and add the following features.- Call the
read_dictionaryfunction, store the returned dictionary in the variable products_dict. - Display the dictionary.
- Open the
request.csvfile for reading. - Skip the first line of the
request.csvfile because the first line contains column headings. - Uses a loop to read and processes each row from the
request.csvfile. Within the body of the loop, your program must do the following for each row:- Use the requested product number to find the corresponding item in the products_dict.
- Print the product name, requested quantity, and product price.
- Call the
- At the bottom of your
receipt.pyfile, add a call to themainfunction. Be certain to protect the call tomainwith anifstatement as taught in the preparation content for week 3.
Testing Procedure
Verify that your program works correctly by following each step in this testing procedure:
- Download the
test_products.pyfile and save it in the same folder where you saved yourreceipt.pyprogram. Run thetest_products.pyfile and ensure that thetest_read_dictionaryfunction passes. If it doesn’t pass, there is a mistake in yourread_dictionaryfunction. Read the output frompytest, fix the mistake, and run thetest_products.pyfile again until the test function passes.> python test_products.py =================== test session starts ==================== platform win32--Python 3.8.6, pytest-6.1.2, py-1.9.0, pluggy rootdir: C:\Users\cse111\week05 collected 1 item test_products.py::test_read_dictionary PASSED [100%] ==================== 1 passed in 0.12s =====================
- Run your program and verify that it prints the products dictionary and requested items as shown in
the sample output below.
> python receipt.py All Products {'D150': ['D150', '1 gallon milk', '2.85'], 'D083': ['D083', '1 cup yogurt', '0.75'], 'D215': ['D215', '1 lb cheddar cheese', '3.35'], 'P019': ['P019', 'iceberg lettuce', '1.15'], 'P020': ['P020', 'green leaf lettuce', '1.79'], 'P021': ['P021', 'butterhead lettuce', '1.83'], 'P025': ['P025', '8 oz arugula', '2.19'], 'P143': ['P143', '1 lb baby carrots', '1.39'], 'W231': ['W231', '32 oz granola', '3.21'], 'W112': ['W112', 'wheat bread', '2.55'], 'C013': ['C013', 'twix candy bar', '0.85'], 'H001': ['H001', '8 rolls toilet tissue', '6.45'], 'H014': ['H014', 'facial tissue', '2.49'], 'H020': ['H020', 'aluminum foil', '2.39'], 'H021': ['H021', '12 oz dish soap', '3.19'], 'H025': ['H025', 'toilet cleaner', '4.50']} Requested Items wheat bread: 2 @ 2.55 1 cup yogurt: 4 @ 0.75 32 oz granola: 1 @ 3.21 twix candy bar: 2 @ 0.85 1 cup yogurt: 3 @ 0.75
Milestone Submission
On or before the due date, return to Canvas and report your progress on this milestone.
Project Completion
Finish your project by completing main function. Ensure your code accomplishes ALL the requirements specified above.
- Remove the line that prints the product dictionary
- Add the remaining code necessary to fulfill the user requirement #4.
Helpful Information
- The preparation content for this lesson explains how to handle exceptions.
Testing
Verify that your program works correctly by following each step in this testing procedure:
- Run your program and verify that it prints a receipt formatted similarly to the one shown below. Your program
must print the current date and time with exactly the same formatting as shown below. Also, verify that your
program computes the number of items, subtotal, sales tax, and total as shown below.
> python receipt.py Inkom Emporium wheat bread: 2 @ 2.55 1 cup yogurt: 4 @ 0.75 32 oz granola: 1 @ 3.21 twix candy bar: 2 @ 0.85 1 cup yogurt: 3 @ 0.75 Number of Items: 12 Subtotal: 15.26 Sales Tax: 0.92 Total: 16.18 Thank you for shopping at the Inkom Emporium. Wed Nov 4 05:10:30 2020
-
Verify that the
exceptblock to handleKeyErrorthat you added to your program works correctly by doing the following:- Temporarily add the following line to the end of your
request.csvfile and then save the file.R002,5
-
Run your program again and verify that it prints an error message like the one shown below.
> python receipt.py Error: unknown product ID in the request.csv file 'R002'
Hint: if you wrote anexceptblock in your program to handleKeyErrorand added "R002,5" to therequest.csvfile and saved therequest.csvfile and ran your program but your program isn’t raising aKeyError, then look in your program to see if you wrote anifstatement before the statement that finds a value in the products dictionary. Look for code similar to this:If your program contains anif prod_num in products_dict: prod_info_list = products_dict[prod_num] ⋮ifstatement similar to the one above, then theifstatement is probably preventing your program from raising aKeyError. Delete theifstatement and unindent the lines of code inside theifstatement and test your program again.
- Temporarily add the following line to the end of your
-
Verify that the
exceptblock to handleFileNotFoundErrorthat you added to your program works correctly by doing the following:-
Temporarily delete or rename the
products.csvfile. -
Run your program again and verify that it prints an error message like the one shown below.
> python receipt.py Error: missing file [Errno 2] No such file or directory: 'products.csv'
-
Exceeding the Requirements
If your program fulfills the requirements for this assignment as described above, your program will earn 93% of the possible points. In order to earn the remaining 7% of points, you will need to add one or more features to your program so that it exceeds the requirements. Use your creativity to add features. Add a comment to the top of your code that explains your enhancement(s).
- Write code to print a reminder of how many days until the New Years Sale begins (Jan 1) at the bottom of the receipt.
- Write code to print a "return by" date that is 9:00 PM 30 days in the future at the bottom of the receipt.
- Challenge: Write code that will give a buy one, get one half off discount for item D083. For example if the customer orders one item it will be full price. If the customer orders two items the first item is full price the second one is discounted 50%. For three items two would be at full price and one at 50% off. Indicate the discounted price on the receipt.
- Write code to print a coupon at the bottom of the receipt. Write the code so that it will always print a coupon for one of the products ordered by the customer.
If you choose to "exceed requirements" place a comment at the top of your code file that describes what you did to enhance your program.
Project Submission
Return to Canvas and upload your receipt.py file for feedback.
If an error prevents your program from running to completion the grader will score your assignment with a 0 and request that you fix and resubmit your program. In other words, rather than submitting a program that doesn't work, it's best to ask for help to understand how to fix the problem before submitting your program.
Useful Links:
- Return to: Week Overview | Course Home