09 Prove Milestone: Text Files
Purpose
Prove that you can write a Python program that reads CSV files and creates, populates, and uses a dictionary.
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 a receipt in the terminal window that lists the purchased items and shows the subtotal, the sales tax amount, and the total.
Assignment
During this milestone, you will write half of a Python program
named receipt.py
that prints a receipt in the terminal
window for a customer’s grocery order. Specifically, by the end of
this milestone, your program must read and process these two CSV
files:
- The
products.csv
file is a catalog of all the products that the grocery store sells. - The
request.csv
file contains the items ordered by a customer.
Help
If you’re having trouble completing this assignment, reading related online documentation, using a generative AI as a tutor, or working with a human tutor will help you complete it.
Helpful Documentation
- 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 lesson 8 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.
Help from an AI Tutor
You could use a generative AI as a tutor to help you write and troubleshoot your program. Bro. Lee Barney created a custom Chat GPT named Pythonista that is designed to focus on Python functions, loops, if statements, and related programming concepts. If your program is generating an error, ask Pythonista a question like this:
I'm writing a Python program that generates a sales receipt for a retail grocery store. When I run my program, it causes the following error. Please suggest some mistakes that might cause this error.
(Copy and paste the error message here.)
You could also ask Pythonista a question about one of your functions, like this:
I wrote the following Python function that is supposed to (type a short description here). However, the function isn't (type what it's not doing correctly here). Please help me fix this function.
(Copy and paste your Python function here.)
Help from a Human Tutor
As a BYU-Idaho campus or online student you can get help from a human tutor to complete your CSE 111 assignments. Each tutor is a current BYU-Idaho student employed by BYU-Idaho. 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. Campus students meet with tutors in the tutoring center. Online students meet with tutors in Zoom. To make an appointment, follow the instructions in the course tutoring guide.
Steps
Do the following:
- Download both of these files:
products.csv
andrequest.csv
and save them in the same folder where you will save your Python program. - Open the
products.csv
file 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.csv
file is unique. This means that your program can read theproducts.csv
file into a dictionary and use the product numbers as dictionary keys. - In VS Code, create a new file and save it as
receipt.py
in the same folder where you saved theproducts.csv
andrequest.csv
files. - In
receipt.py
, write code to open theproducts.csv
file and read its rows into a compound dictionary. Because you want to write a well-organized program, does it seem like a good idea to put this code into a function? Will this function need any parameters? As part of the team activity for this lesson, you already wrote a similar function. - Open the
request.csv
file in VS Code and examine it. Notice that each row contains only two values, a product number and a quantity. Notice also that product number D083 appears twice in the file because the customer who created the order in therequest.csv
file added four yogurts to his order and then later added three more yogurts to his order. Because product numbers may appear multiple times in therequest.csv
file, your program should not read the contents ofrequest.csv
into a dictionary. In your
receipt.py
program, write code to open therequest.csv
file and read and process each row. Because you want to write a well-organized program, does it seem like a good idea to put this code into a function? Will this function need any parameters? Your code that reads therequest.csv
file must do the following:- Open the
request.csv
file for reading. - Skip the first line of the
request.csv
file because the first line contains column headings. - Use a loop that reads and processes each row from the
request.csv
file. 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.
- In your
receipt.py
program, write another function namedmain
that does the following:- Calls your function that reads from the
products.csv
file into a compound dictionary and stores the returned compound dictionary in a variable named products_dict. Does your function have any parameters? If so, did you add arguments to the function call? - Prints the products_dict.
- Calls your function that reads and processes the
request.csv
file. Does your function have any parameters? If so, did you add arguments to the function call?
- At the bottom of your
receipt.py
file, add a call to themain
function. Be certain to protect the call tomain
with anif
statement as taught in the preparation content for lesson 5.
Testing Procedure
Verify that your program works correctly by following each step in this testing procedure:
- 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
Common Mistake
Counting Only Three D083 Yogurts
Because product number D083 appears twice in the
request.csv
file, your program should not read the
request.csv
file into a dictionary. Recall that each
key in a dictionary is unique. If your program reads the
request.csv
file into a dictionary, when your program
reads line 3 of the request.csv
file, your program will
store a request for four yogurts into the dictionary. Then when it
reads line 6 of the request.csv
file, your program will
replace the request for four yogurts with a request for three
yogurts. In other words, if your program reads the
request.csv
file into a dictionary, your program will
think that the customer ordered only three yogurts instead of the
seven (4 + 3) that he ordered.
Therefore, your program should not read the request.csv
file into a dictionary but should instead read and process each row
similar to example 3 in the
preparation content for this lesson.
Submission
On or before the due date, return to I‑Learn and report your progress on this milestone.