Register Login

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

Updated Jan 12, 2020

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

You can multiply an integer value and a float value by converting the integer into a float. But if you are trying to multiply a float and a value that is not an integer or string, you will encounter an error. An error called "TypeError can't multiply sequence by non-int of type 'float'" will be raised.

The easiest way to resolve this is by converting the string into a float or integer and then multiplying it.

The following code will throw the error:

# Declare variables 
val1 = '10'
val2 = 1.2

# Multiply variables 
result = (val1*val2)

# Print Output 
print('Multiply of val1 and val2: ',result);

Output

Traceback (most recent call last):
  File "multiply.py", line 3, in <module>
    result = (val1*val2)
TypeError: can't multiply sequence by non-int of type 'float'

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

In the above example, we have declared an integer variable as a string

val1 = '10' # Declare variables as string

Due to which while doing the multiplication between string and float variable it raised an error.

To solve this error, you need to change the code where you are multiplying "val1" with "val2" Here is the fix:

result = (float(val1)*val2)

This converts the "val1" variable into a float and then multiplies it with "val2"  

Correct Code:

# Declare variables 
val1 = '10'
val2 = 1.2

# Multiply variables 
result = (float(val1)*val2)

# Print Output 
print('Multiply of val1 and val2: ',result);

 


×