Python 3.6, problem - See attached file and code. very perplexed. I think my sol
ID: 3717894 • Letter: P
Question
Python 3.6, problem - See attached file and code. very perplexed. I think my solutions are close but just cant get my finger on it. Im not certain Im splitting this correctly and program not writing to new file. ALso line 33 error message, write statement takes 1 argument and I have two ; this is happening when i add percision (.ex, .line 33....str(inventoryValue[i]),',.2f')) and probably the next line/statement as well. Please take a look and offer some suggestions/instructions to help me understand what i've done wirong and how i may correct my errors. Thanks.
FILE:
228,SEA-13189,Pepper,Florida,8 oz.,Seasoning,8.75
135,SEA-15688,Pepper,Lemon,4 oz.,Seasoning,6.25
146,SPI-23688,Curry Powder,Hot,8 oz.,Spice,9.99
135,SPI-25678,Curry Powder,Sweet,8 oz.,Spice,9.99
156,HER-32544,Mint,Spearmint,8 oz.,Herb,10.29
156,HER-34266,Basil,French,8 oz. ,Herb,10.19
266,SPI-34878,Ginger,Cracked,8 oz.,Spice,7.89
177,SEA-34982,Jerk,Chicken and Fish,4 oz.,Seasoning,5.45
245,SEA-35677,Jerk,Pork,8 oz.,Seasoning,9.85
PROBLEM:
# Instructors folder CSV file called Inventory.txt.
# Read contents of file and out put a file called InventoryX.txt.
# File will contain lines displaying the following:
# COUNT of items, ITEM CODE, GENERAL name, SPECIFIC name, SIZE, TYPE, PRICE per.
# Example of line: 228, SEA-13189, Pepper, Florida, 8 oz., Seasoning, 8.75
# Display as Florida Pepper <space> value of inventory.
# where the name will be displayed with the SPECIFIC portion followed by the
# GENERAL portion of name seperated by one space and amount will appear with
# two decimal digits with commas in neatly arranged (decimals alligned, columnar
# display output file. In addion, write one additional line to the file which will be
# TOTAL INVENTORY which is total of the value of all items. Allign all in
# columns, commas as necessary, decimal places alligned.
Here's my code:
specificName = []
generalName = []
inventoryValue = []
readFile = open('Inventory.txt','r')
for line in readFile.readlines():
cols = line.split(',')
specificName.append(cols[3])
generalName.append(cols[2])
inventoryValue.append(int(cols[0])*float(cols[6]))
#writing results to output file
InventoryX = open('Inventory.txt','w')
totalInvValue = 0
for i in range(0,len(specificName)):
InventoryX.write(specificName[i] + ''
+ generalName[i]+' '+ (str(inventoryValue[i]),',.2f'))
totalInvValue+=inventoryValue[i]
InventoryX.write('Total Inventory '+ (str(totalInvValue),',.2f'))
InventoryX.close()
Explanation / Answer
Your code is quite correct but there are some mistakes which I recovered for you. I put # and blod it ,where there are changes. You create a file as Invetory.txt and paste the data, but InventoryX.txt will be created by the program.Please check the program and tell me in comments is it ok or not.
specificName = []
generalName = []
inventoryValue = []
readFile = open('Inventory.txt','r')
for line in readFile.readlines():
cols = line.split(',')
specificName.append(cols[3])
generalName.append(cols[2])
inventoryValue.append(int(cols[0])*float(cols[6]))
#writing results to output file
#InventoryX = open('Inventory.txt','w')
InventoryX = open('InventoryX.txt','w')
totalInvValue = 0
for i in range(0,len(specificName)):
InventoryX.write(specificName[i]+' '+generalName[i]+ ' '+str(round(inventoryValue[i],2))+' ')
totalInvValue+=inventoryValue[i]
# + ''+generalName[i]+' '+ (str(inventoryValue[i]),',.2f')
# totalInvValue+=inventoryValue[i]
#InventoryX.write('Total Inventory '+ (str(totalInvValue),',.2f'))
InventoryX.write(' Total Inventory:'+ str(totalInvValue))
InventoryX.close()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.