To preface this, I am by no means a programmer, and learning 1 code language was required for my college major. We use Pyhthon in Thonny.
I'm tasked with writing code that "will process inventory from a file called ABC_Inventory.txt. The file contains Item ID, Description and list price stored on a separate line in the file. The program should display the contents of each record and then calculate and display the average list price."
I have written this code:
Inventory = open('ABC_Inventory.txt', 'r')
ItemID = ""
ItemDesc = ""
ItemPrice = 0.00
TotalPrice = 0.00
ItemAverage = 0.00
NumberOfItems = 0
while True:
ItemID = Inventory.readline().strip()
ItemDesc = Inventory.readline().strip()
ItemPrice = float(Inventory.readline().strip())
TotalPrice += ItemPrice
NumberOfItems += 1
ItemAverage = TotalPrice / NumberOfItems
if ItemID == "":
break
print(ItemID + " is " + ItemDesc + " and is priced at: " + str(ItemPrice))
Inventory.close()
print("Average list price is : " + ItemAverage)
I am getting the error:
Traceback (most recent call last):
ItemPrice = float(Inventory.readline().strip())
ValueError: could not convert string to float:
What am I doing wrong? Any help would be greatly appreciated!
Here is the Inventory file, if that matters:
BP2500
3-PERSON TENT
75.0
BP1000
LOUNGE CHAIR
50.0
BP3000
PROPANE HEATER
149.0
BP0900
HIKING BOOTS
120.0
BP0950
BACKPACK
100.0
BP3050
GPS TRACKER
130.0
'if-logicdirecly beneathItemID = Inventory.readline().strip()and addelse clausefor the rest of thewhile block. For the last line, change it toprint("Average list price is : " + str(ItemAverage)).