Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The previous reponce i got did not make this code readable, and it was very hard to follow. I would like to have this code

The previous reponce i got did not make this code readable, and it was very hard to follow. I would like to have this code written out and explained what we did . How were you able to read the file with this program what steps were taken to complete this. The next code gave me errors, can someone please help me.

[sales-invoice-finder.py] You are working for a computer parts manufacturer that needs a new program to find sales information based on one of two pieces of information

  • an invoice identifier (id) or
  • a customer's last name (lname)

Your company logs each parts sale in an Excel file saved as a CSV file called `sales_data.csv` (NOTE: if you think no company would ever do this, you are incorrect: it has been done). The first line in that file contains the column headers: textual descriptions of what data is in each column. The columns are invoice id, a customer's first name, last name, part number, quantity, and total.

Your goal is to make this file searchable. Your program should prompt the user for the following inputs, in this order:

  • Whether they want to search using an invoice id (input of `id`) or by a customer's last name (input of `lname`). The program should reject any input that is not `id` or `lname`, forcing the user to choose one of those two options.
  • The search term, either an `id` value or an `lname` value

After the user enters these inputs, the program should open the data file (note that the user does not input the data file), then search within the chosen column (i.e., either `id` or `lname`, but not both) for the input value. If the program finds a match in any invoice recrords, then it it should print (not save) all recorded invoices that match. Finally it should print the total number of records found that match the search term.

Here are three examples:

Search by invoice id (id) or customer last name (lname)? firstname ERROR: You must enter either 'id' for invoice id search or 'lname' for customer last name search Search by invoice id (id) or customer last name (lname)? lname Enter your search term: Hutz 87681,Lionel,Hutz,218,1,50.83 

# sales_data.csv will be like following

invoice_id,first_name,last_name,part_number,quantity,total 87621,Lionel,Hutz,218,1,50.83 34018,Lionel,Hutz,112,3,88.88 34018,Lachim,langa,386,3,86.04

#python program

import csv

ip=""

while True:

ip = input("Search by invoice id (id) or customer last name (lname)?")

if not (ip != "id" and ip!="lname"):

break

else:

print("ERROR: You must enter either 'id' for invoice id search or 'lname' for customer last name search")

search_txt = input("Enter your search term:")

ind = 0

if ip == "lname":

ind = 2

with open('sales_data.csv') as csv_file:

csv_reader = csv.reader(csv_file,delimiter=',')

coun=0

for row in csv_reader:

if search_txt == row[ind]:

coun = coun+1

for i in range(len(row)-1):

print(row[i],end=',')

print(row[len(row)-1])

print(str(coun)+" records found.")

# If you got the answer, please upvote :)

 34018,Lionel,Hutz,112,3,88.88 34018,Lionel,Hutz,386,3,86.04 34018,Lionel,Hutz,216,1,53.54 4 records found.
Search by invoice id (id) or customer last name (lname)? id Enter your search term: 93303 93303,Frank,Grimes,392,2,90.74 93303,Frank,Grimes,142,3,73.2 93303,Frank,Grimes,353,1,45.87 3 records found.
Search by invoice id (id) or customer last name (lname)? lname Enter your search term: Maryville 0 records found.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

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

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions