Skip to content Skip to sidebar Skip to footer

Python Typeerror When Dividing A Raw Input Variable By A Number

I want to convert an entered lb weight to kg and I get the following error... TypeError: unsupported operand type(s) for /: 'unicode' and 'float' My code: lbweight = raw_input('C

Solution 1:

raw_input returns a string, you should convert the input to float using float():

float(raw_input("Current Weight (lb): "))

Solution 2:

Pay attention to the error message TypeError: unsupported operand type(s) for /: 'str' and 'float'

>>> kgweight = lbweight/2.20462

Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    kgweight = lbweight/2.20462
TypeError: unsupported operand type(s) for /: 'str'and'float'>>> 

So if 2.20462 is a float then which is a string here? What does the documentation say about raw_input?

If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised.

Solution 3:

That's because with raw_input, the input is raw, meaning a string:

lbweight = float(raw_input("Current Weight (lb): ") )

kgweight = lbweight/2.20462

Post a Comment for "Python Typeerror When Dividing A Raw Input Variable By A Number"