How To Deal With TypeError: can’t multiply sequence by non-int of type float in Python

There are many reasons we have to multiply data with a float type, such as calculating the value of invoices. During the calculation, you may encounter the TypeError: can’t multiply sequence by non-int of type float. This Error occurs when we have not converted the data type to integer before multiplying by float, this article will clearly explain the cause and how to fix it.

What causes TypeError: can’t multiply sequence by non-int of type float?

The TypeError: can’t multiply sequence by non-int of type float occurs because we perform multiplication between a string or a List with a variable of type float. This Error can be caused by receiving a value from the user or multiplying a function’s return value by the float data type. Below is an example of this Error that occurred.

Error example:

priceArray = [199, 299, 399, 499]
quantity = 4.5

# Error occurred when performing multiplication between a List and float
result = priceArray * quantity
print(result)

Output:

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

In this case, you can use int(), round(), or multiply each element in the List with quantity.

Errors can also occur when we multiply a series of string values with data type float. The following example describes a List multiplied by a float

Error example:

fruits = ["apple", "banana", "cherry"]
quantity = 4.5

# Error occurred when performing multiplication between List of string and float
result = fruits * quantity
print(result)

Output:

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

To fix the error in the above example, you can use the int() or round() function.

When getting value from the input() function, we always get string data type. This Error occurs when we multiply this string value by a float.

Error example:

price = input("Please enter price")
quantity = 4.5

# Error occurs when we multiply this string value by a float
result = price * quantity
print(result)

Output:

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

You can use the int() or float() function to convert a string value entered by the user into a number.

Generally, this Error occurs when we multiply the unconverted value by the float. So how to solve it.

Solution for the TypeError: can’t multiply sequence by non-int of type float

To solve this Error, we need to convert the value to integer type or float type before multiplying by a float. If we don’t know the data type, we can use the type() function.

Use int() function

If the data type is a float, we will use the int() function to convert the float to an integer.

Example:

priceArray = [199, 299, 399, 499]
quantity = 4.5

# Use the int() function to convert the float to an integer
result = priceArray * int(quantity)
print(result)

Output:

[199, 299, 399, 499, 199, 299, 399, 499, 199, 299, 399, 499, 199, 299, 399, 499]

Use round() function

You can use the round() function to round a float variable

Example:

fruits = ["apple", "banana", "cherry"]
quantity = 4.5

# Use the round() function to round a float variable
result = fruits * round(quantity)
print(result)

Output:

['apple', 'banana', 'cherry', 'apple', 'banana', 'cherry', 'apple', 'banana', 'cherry', 'apple', 'banana', 'cherry']

Using the float() function

When using the input() function to get data from the user, we always get a string data type. So to avoid this Error, we need to convert to an integer or a float before multiplying by a float.

Example:

# Get value "199" from user
price = input("Please enter price: ")
quantity = 4.5

# Use float() function to convert string to a float before multiplying by a float.
result = float(price) * quantity
print(result)

Output:

Please enter price: 123
553.5

Summary

Through this article. We have know several ways to avoid errors like converting from string to integer or float to integer. The best way to tackle these problems is to know what data type before multiplying it by a float. Hope you enjoyed this article, good luck.

Leave a Reply

Your email address will not be published. Required fields are marked *