Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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:

  1. Print the store name at the top of the receipt.
  2. Print the list of ordered items.
  3. Sum and print the number of ordered items.
  4. Sum and print the subtotal due.
  5. Compute and print the sales tax amount. Use 6% as the sales tax rate.
  6. Compute and print the total amount due.
  7. Print a thank you message.
  8. Get the current date and time from your computer's operating system and print the current date and time.
  9. Include a try block and except blocks to handle FileNotFoundError, PermissionError, and KeyError.
  10. 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... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Industrial Relations in Canada

Authors: Fiona McQuarrie

4th Edition

978-1-118-8783, 1118878396, 9781119050599 , 978-1118878392

More Books

Students also viewed these Programming questions

Question

=+b. What are the penalties for exceeding the upper specification?

Answered: 1 week ago

Question

=+a. The number of flaws per square foot in a large sheet of metal

Answered: 1 week ago