Cjkblue
Cjkblue

Reputation: 3

Cant multiply sequence by non-int of type 'float'

I'm trying to make a Inch to centimetre (And vice versa) converter with Python.

print "CM TO INCH (VV) CONVERSION. ENTER SPECIFIED NUMBER:"
inches = 2.54 #centimetres
cms = 0.393701 #inches
number = raw_input()
answercms = number * inches
answerinches = number * cms
print "%d centimetres are %d inches. %d inches are %d centimetres." % (number, answerCms, number, answerinches)

After running the script in Powershell it gives this error:

Traceback (most recent call last):
   File "inchcm.py", line 5, in <module>
     answercms = number * inches
TypeError: can't multiply sequence by non-int of type 'float'

I know this question has come up once or twice but I dont understand the answer.

Upvotes: 0

Views: 1068

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200453

raw_input() returns a string. You can't multiply a string with a number, so you need to convert the number string to an actual number format first, for instance with the float() function:

number = raw_input()
answercms = float(number) * inches

You can use type() for checking the actual type of a variable:

>>> type(number)
<type 'str'>
>>> type(float(number))
<type 'float'>

Upvotes: 2

Related Questions