# Multiply Sequence by Float Error in Python

There are two types of numbers in Python:

• Integers – positive and negative whole numbers, e.g., -6, 0, and 9,
• Floats – positive and negative decimal numbers, for example, -2.3, 0.0, and 3.142

Python language allows a user to multiply an integer with a string, list, or tuple to generate a repeated sequence; for example,

Output:

```TomaszTomaszTomaszTomaszTomaszTomaszTomasz
[1, 'Decker', 3.4, 1, 'Decker', 3.4, 1, 'Decker', 3.4, 1, 'Decker', 3.4, 1, 'Decker', 3.4]
(2, None, [0, 1, 2], 2, None, [0, 1, 2], 2, None, [0, 1, 2], 2, None, [0, 1, 2])```

But what happens if we multiply any of the sequences above with a float? That is where the “Multiply Sequence by Float” Error comes in.

## What causes of “Multiply Sequence by Float” Error and How to Solve it

The “Multiply Sequence by Float” Error is a TypeError raised when we try multiplying a sequence (e.g., string, list, tuple) with float. For example,

Output:

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

In the case above, Python throws a TypeError exception – an exception thrown when we try to operate on datatypes that are not compatible with that specific operation.

The error message explains the error better – we cannot multiply a sequence (in this case, string) with a float (an integer was expected).

Several cases can lead to “TypeError: can’t multiply sequence by non-int of type ‘float'”. Let’s discuss some of them.

## Case 1: Multiplying a String With a Float

An example falling into this category is already given above. Here is another example.

Output:

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

A reminder: a string is any character or group of characters enclosed in double or single quotes. 5.6 is a string.

### Solution: Convert string to float or integer

If you have a numerical string like in “5.6”*3.2, convert the string into float and multiply the two numbers, that is,

Output:

`17.919999999999998`

Another scenario, in this case, is when we want to repeat a sequence like in “Tomasz”*3.2. In that case, we need to convert the multiplication factor into an int, that is,

Output:

`TomaszTomaszTomasz`

## Case 2: Attempt to Multiply a List or Tuple With a Float

This fits a situation where you want to multiply every list element with a float. For example, you might do [2, 4, 5]*3.5 expecting to get [ 7. 14. 17.5], instead, you will get this

Output:

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

### Solution: Use a list comprehension or NumPy package

If you want to multiply each list element or type with a given number, you can use list comprehension, as shown below.

Output:

`[7.0, 14.0, 17.5]`

Alternatively, you can use the NumPy package. This is especially useful if you have a huge list. The package supports vectorization, which may speed up your computation in such a case.

Output:

`[ 7.  14.  17.5]`

Note: You may need to install NumPy, in this case, using pip with the command:

pip install numpy

## Case 3a: Multiplying Elements of Two lists or Tuples

This does not lead to the exact error we discuss in this article, but a variant. In this case, a user wants to multiply elements at corresponding positions in two lists or tuples.

For example, you may want [2, 3, 4]*[2, -2, 3] to yield [4, -6, 12] but, instead, you will get something like this

Output:

`TypeError: can't multiply sequence by non-int of type 'list'`

### Solution: Using list comprehension, map() function, or NumPy

There are three options here. The first one is to use list comprehension, as follows.

Output:

`[4, -6, 12]`

The zip() function allows us to pair the corresponding values from the two lists, and list comprehension iterates through each of the pairs multiplying them.

The second solution, in this case, is to use map() with the lambda function. That is,

Output:

`[4, -6, 12]`

Lastly, you can use NumPy. It makes things a lot easier, as seen in the example below.

Output:

`[ 4 -6 12]`

## Case 3b: Multiplying a List of Numerical Strings with a Float

This is a slight twist of Case 3a. In this case, we have a list of numerical strings, e.g. [“10.2”, “7.6”, “-3.4”], and we want to multiply each value by a float, say 2.1.

As expected, [“10.2”, “7.6”, “-3.4”]*2.1 will yield an error.

Output:

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

### Solution: We need to convert each value of the list into float or int

We can conveniently convert each list item into a number using the map() function introduced in Case 3a. That can be done as follows.

Output:

`[10.2, 7.6, -3.4]`

## Case 4: Using the input() Function

The input() function in Python 3 and raw_input() function in Python convert every input into a string, even if you insert a number in the prompt. For example,

Output:

```Enter the salary: 2719
2719
<class 'str'>```

When I executed the code above, I was issued a prompt, and I entered the integer 2719 and hit Enter, but input() converted the input value into a string.

The problem will come when we insert an input value and try to multiply it with a float directly. For example, in the code snippet below, I want to take the input salary and compute an increment (11% of the salary).

Output:

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

### Solution: Convert the input value into a float or int

That can be done using float(), int(), or eval() functions, as shown below.

Output:

```Enter the salary: 2719
299.09```

Note: Use int(input(<prompt>)) only when you are sure that the inputs are integers; otherwise, you will get an error.

## Conclusion

This article discussed the causes and solutions to the “TypeError: can’t multiply sequence by non-int of type ‘float'” Error in Python. After going through the guide, you should be able to solve the error and its variants whenever it occurs. The variants of this error include:

• TypeError: can’t multiply sequence by non-int of type ‘float’
• TypeError: can’t multiply sequence by non-int of type ‘string’
• TypeError: can’t multiply sequence by non-int of type ‘list’ (See Case 3a)
• TypeError: can’t multiply sequence by non-int of type tuple (See Case 3a)