Question
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
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 try block and except blocks to handle FileNotFoundError, PermissionError, and KeyError.
- 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.
This is the code of the previous lesson
import csv
def read_dictionary(filename, key_column_index):
"""
Read the contents of a CSV file into a compound dictionary and return the dictionary.
Parameters:
filename: the name of the CSV file to read.
key_column_index: the index of the column to use as the keys in the dictionary.
Return: a compound dictionary that contains the contents of the CSV file.
"""
products_dict = {}
with open(filename, 'r') as file:
csv_reader = csv.reader(file)
next(csv_reader)
for row in csv_reader:
key = row[key_column_index]
value = [row[i] for i in range(len(row))]
products_dict[key] = value
return products_dict
def main():
products_dict = read_dictionary('products.csv', 0)
print(products_dict)
with open('request.csv', 'r') as file:
csv_reader = csv.reader(file)
next(csv_reader)
for row in csv_reader:
product_num = row[0]
quantity = int(row[1])
product_info = products_dict[product_num]
product_name = product_info[1]
product_price = float(product_info[2])
total_price = quantity * product_price
print(f'{product_name} ({quantity}): ${total_price:.2f}')
if __name__ == '__main__':
main()
Step by Step Solution
3.34 Rating (148 Votes )
There are 3 Steps involved in it
Step: 1
Answer import csv import datetime def readdictionaryfilename keycolumnindex existing code for readin...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started