-3
votes

I'm running Python 2.7.10. I have the following code block from a program I'm working on.

with open('inventory.txt', 'r+') as f:
  inventory = {}
  while True:
    item = raw_input('Item: ')
    inventory[item] = raw_input('Price: ')
    if item == '':
      del inventory['']
      break

  inv = str(inventory)
  f.write(inv).rstrip()
  print inventory
  print inv
  print f.read()

What it does is prompts a user for an item and price and then it stores all of those as key/value pairs and then writes that final dictionary onto a second text file. On line 5 however it seems like the only type of input it will except there is a string. I've tried to surround the raw_input by a float() and tried to make extra variables to no avail. I was able to wrap the raw_input in a int() and it work so it threw me off.

When I change line 5 to inventory[item] = float(raw_input('Price: ')) I get the following error:

File "C:\Users\Jarrall\Desktop\store\script.py", line 5, in <module>
inventory[item] = float(raw_input('Price: '))
ValueError: could not convert string to float: 

What must I change about the code so that when a user enters a numerical value on line 5 it saves to the dictionary as such instead of a string (currently)?

2
inventory[item] = float(raw_input('Price: ')) - Bob Dylan
^^^ Even so, using float for money is a bad idea. Use Decimal or just integers - Bob Dylan
As I said, I tried that. It gave me an error. Please do not vote it down as it's a serious question from a beginner, you've not even answered my question, and I don't have many reputation points to lose. @BobDylan - Jarrall Barnett
@JarrallBarnett Bob's comment is correct, convert it to a float using the float() function. Or alternatively write a function to accept floats from input and use that. - Chad S.
A) I didn't downvote this initially (that DV wasn't mine). B) that does work. The issue must be that the user is not entering a float. Casting it manually and ensuring it is actually a float first is a better approach. C) if you received an error when trying to do it, you need to show the error here also so we can better help you. D) Now I'm going to downvote this. - Bob Dylan

2 Answers

5
votes

The short answer is to use float(raw_input('Price: ')), but it might be better to write a method to handle the inputing of floats (and retry until you get what you need).

def input_float(prompt):
    while True:
        try:
            return float(raw_input(prompt))
        except ValueError:
            print('That is not a valid number.')

then use the method

inventory[item] = input_float('Price: ')
1
votes

Try using my code. I just made it and started browsing the web for people that. Disclaimer:I made it in Python 3.7 so I am not sure it will work with your problem.

#Get Input
User_Input_Number_Here = input ("Enter a number: ")
try:
#Try to convert input to Integer
   User_Input_Converted_Here = int(User_Input_Number_Here)
#It worked, now do whatever you want with it
   print("The number is: ", User_Input_Converted_Here)
except ValueError:
#Can't convert into Integer
    try:
#Try converting into float
       User_Input_Converted_Here = float(User_Input_Number_Here)
#It worked, now do whatever you want with it
       print("The number is: ", User_Input_Converted_Here)
    except ValueError:
#Can't convert into float either. Conclusion: User is dumb
#Give custom error message
       print("Are you by any chance...\nRetarded?")