SuckMyCode
SuckMyCode

Reputation: 1

TypeError: can't multiply sequence by non-int of type 'float' 3.0

Getting a TypeError: can't multiply sequence by non-int of type 'float'error. any help appreciated.

userEuro = raw_input("What amount in Euro do you wish to convert?")

    USD = userEuro * 1.3667

Upvotes: 0

Views: 717

Answers (3)

Vivek Sable
Vivek Sable

Reputation: 10223

raw_input() return value which user enter in a string

So type of userEuro variable is string, you can check by type() method

>>> userEuro = raw_input("What amount in Euro do you wish to convert?")
What amount in Euro do you wish to convert?1.2
>>> type(userEuro)
<type 'str'>
>>> userEuro
'1.2'

Do type casting to convert from string to float.

>>> float(userEuro)
1.2
>>> 

Do exception handling during type casting because user may enter wrong value as input.

>>> userEuro = raw_input("What amount in Euro do you wish to convert?")
What amount in Euro do you wish to convert?ab
>>> userEuro
'ab'
>>> float(userEuro)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: ab

>>> try:
...    float(userEuro)
... except ValueError:
...    print "Wrong value for type casting: ", userEuro
... 
Wrong value for type casting:  ab
>>> 

What is TypeError: can't multiply sequence by non-int of type 'float' exception:

When we multiply string by float value then this exception is coming.

>>> "1.2" * 1.3667
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'float'

String is multiple by integer value

>>> "test" * 2
'testtest'

But String is not multiple by integer or float value

>>> "test" * '2'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'str'
>>> 

Python 3.x

raw_input() method is remove from the Python 3.x,

raw_input() for Python 2.x

input() for Python 3.x

Upvotes: 1

astrosyam
astrosyam

Reputation: 867

Change the second line to :

USD = float(userEuro) * 1.3667

raw_input accepts the user input as a String. You will need to convert it into float before performing the above mathematical operation on it.

Upvotes: 0

DeepSpace
DeepSpace

Reputation: 81604

userEuro is a string. You should convert it to a float by using float(raw_input()).

FYI, a string can be multiplied but only by an integer. This results in a string concatenation:

print 'a' * 2
>> 'aa

Upvotes: 0

Related Questions