Sovi.AI - AI Math Tutor

Scan to solve math questions

QUESTION IMAGE

complete the code to finish this program to analyze the inventory for a…

Question

complete the code to finish this program to analyze the inventory for a store that sells purses and backpacks. each record is composed of the catalog number, the type of item, its color, the length, width, height, and the quantity in stock. sample rows of the file are below. 234,purse,blue,12,4,14,10 138,purse,red,12,4,14,4 934,backpack,purple,25,10,15,3 925,backpack,green,25,10,15,7 import filein = open(\data/bags.txt\,
\) countpurse = 0

Explanation:

Step1: Identify the missing import

Since we are dealing with file - reading and likely need to process the data further, we might need the csv module if the data is comma - separated (which it is).
import csv

Step2: Read the file using csv reader

We can use the csv.reader to read the file line by line and process the data.
fileIn = open("data/bags.txt", "r")
countPurse = 0
reader = csv.reader(fileIn)
for row in reader:
if row[1] == "purse":
countPurse += 1
print(countPurse)
fileIn.close()

Answer:

The completed code:
import csv
fileIn = open("data/bags.txt", "r")
countPurse = 0
reader = csv.reader(fileIn)
for row in reader:
if row[1] == "purse":
countPurse += 1
print(countPurse)
fileIn.close()