W05 Project: Grocery Store
Purpose
Prove that you can write a Python program that handles exceptions, including FileNotFoundError, PermissionError, and KeyError.
Problem Statement
A local grocery store subscribes to an online service that enables its 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. The store needs 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.
Assignment
During the prove milestone for the previous lesson, you wrote the part of this program that reads and processes two CSV files, one named
products.csv that contains a catalog of products and one named request.csv that contains a customer’s order. During
this prove assignment, you will add code to finish printing a receipt and to handle any exceptions that might occur while your program is
running. Specifically, your program must do the following:
-
Print the store name at the top of the receipt.
-
Print the list of ordered items.
-
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.
Helpful Documentation
-
The prove milestone describes the two CSV files that your program must process.
-
The preparation content for this lesson explains how to handle exceptions.
-
The
datetime.now() methodfrom the standard Pythondatetimemodule will get the current date and time from your computer’s operating system. Here is an excerpt from the official documentation for thedatetime.nowmethod:-
datetime.now(tz=None) -
Return the current local date and time.
tz is optional, but if it is not
None, it must be atzinfo(time zone information) object
These two Microsoft videos explain how to use methods from the standard
datetimemodule.-
Date data types (8 minutes)
-
Demonstration: Dates (9 minutes)
The following Python code imports the
datetimeclass from thedatetimemodule and calls thedatetime.nowmethod to get the current date and time from a computer’s operating system. Then it uses an f-string to format and print the current date and time.# Import the datetime class from the datetime # module so that it can be used in this program. from datetime import datetime # Call the now() method to get the current # date and time as a datetime object from # the computer's operating system. current_date_and_time = datetime.now() # Use an f-string to print the current # day of the week and the current time. print(f"{current_date_and_time:%A %I:%M %p}")> python example_1.py Tuesday 01:23 PM
After the computer executes line 8 in the above code, the variable current_date_and_time will hold the current date and time. Within the f-string at line 12, the string sequences that begin with the percent symbol (%) are called format codes. The format codes and their meaning are listed in the official Python
datetimereference. As shown in the terminal window above, the previous example code will print the current day of the week and time to the terminal window. -
Help from a Tutor
As a BYU-Pathway student you can get help from a tutor to help you complete your CSE 111 assignments. Each tutor is a current student employee. Meeting with a tutor is free. It will not cost you any money to meet with a tutor. To get help from a tutor, you simply make an appointment and then meet with the tutor. Tutors typically use Zoom to meet with students. To make an appointment, use the tutoring link on the course home page.
Testing Procedure
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.
-
-
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 in the previous prove milestone and the Assignment section 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. Here are a few suggestions for additional features that you could add to your program if you wish.
-
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.
Submission
To submit your program, return to Canvas and do these two things:
-
Upload your program (the .py file) for feedback.
-
Add a submission comment that specifies the grading category that best describes your program along with a one or two sentence justification for your choice. The grading criteria are:
-
Some attempt made
-
Developing but significantly deficient
-
Slightly deficient
-
Meets requirements
-
Exceeds requirements
-
Useful Links:
- Return to: Week Overview | Course Home