0

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
1
  • 3
    Put that 'if-logic direcly beneath ItemID = Inventory.readline().strip() and add else clause for the rest of the while block. For the last line, change it to print("Average list price is : " + str(ItemAverage)). Commented Nov 3, 2020 at 6:12

3 Answers 3

2
while True:
    ItemID = Inventory.readline().strip()
    if ItemID == "":
        break
    else:
        ItemDesc = Inventory.readline().strip()
        ItemPrice = float(Inventory.readline().strip())
        TotalPrice += ItemPrice
        NumberOfItems += 1
        ItemAverage = TotalPrice / NumberOfItems
        
        print(ItemID + " is " + ItemDesc + " and is priced at: " + str(ItemPrice))
    
Inventory.close()

print("Average list price is : " + str(ItemAverage))
BP2500 is 3-PERSON TENT and is priced at: 75.0
BP1000 is LOUNGE CHAIR and is priced at: 50.0
BP3000 is PROPANE HEATER and is priced at: 149.0
BP0900 is HIKING BOOTS and is priced at: 120.0
BP0950 is BACKPACK and is priced at: 100.0
BP3050 is GPS TRACKER and is priced at: 130.0
Average list price is : 104.0
[Finished in 0.3s]
Sign up to request clarification or add additional context in comments.

1 Comment

the unnecessary conversion to str in print can be avoided by using the string formatting like "Average list price is : {}".format(ItemAverage)
1

I think the program looks for every line for conversion which is absolutely wrong. If you know that the value will be float, just try this code which ignores the error and adds a line in the program before the average:

ItemID = ""
ItemDesc = ""
ItemPrice = 0.00
TotalPrice = 0.00
ItemAverage = 0.00
NumberOfItems = 0

while True:
    ItemID = Inventory.readline().strip()
    ItemDesc = Inventory.readline().strip()
    try:
        ItemPrice = float(Inventory.readline().strip())
    except:
        print()
    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 : " + str(ItemAverage))```

Comments

0

The float() method only allows you to convert strings that appear like floats. This means that you cannot convert a value if:

A value contains spaces
A value contains a comma
A value contains non-special characters (i.e. “inf” is a special character, but “fd” is not)

The “valueerror: could not convert string to float” error is raised if you fail to meet any one of the three above criteria. This is because Python cannot convert a value to a float unless that value appears in a particular way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.