[Solved] Python can’t Multiply Sequence by non-int of type ‘float’

The TypeError: can’t multiply sequence by non-int of type ‘float’ occurs if we use the multiply operator between a string and float value. 

In this tutorial, we will learn what exactly TypeError: can’t multiply sequence by non-int of type ‘float’ error means and how to resolve this TypeError in your program with examples.

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

Python is one of the best programming languages because of its features and simplicity. One such fantastic feature in Python is we can multiply strings with numbers.

Multiplying string with an integer

Let’s take an example to demonstrate multiplying string with numbers.

The other popular programming languages will never let you to multiple strings and integers. However, we can perform a multiplication between string and integer in Python. After the multiplication, the string is repeated for n times, as shown below.

text = "ItsMyCode"
n = 3
print(text*n)

Output

ItsMyCodeItsMyCodeItsMyCode

Here the string “ItsMyCode” is repeated multiplied by three and repeated three times in the output.

Note: We cannot multiply string with another string, if we do Python interpreter will throw TypeError: can't multiply sequence by non-int of type 'str'. 

If we try to multiply the string with non-int, let’s say, a floating-point value, then the Python interpreter will throw TypeError: can’t multiply sequence by non-int of type ‘float’.

Multiplying string with a floating-point 

You cannot multiply a string with a non-integer value in Python. Hence if we multiply a string with a floating-point value, we get the error can’t multiply sequence by non-int of type ‘float’.

Let’s take an example to demonstrate multiplying string with a floating-point value.

text = "ItsMyCode"

# floating-point value
n = 3.0
print(text*n)

Output

Traceback (most recent call last):
  File "C:\Personal\IJS\Code\program.py", line 3, in <module>
    print(text*n)
TypeError: can't multiply sequence by non-int of type 'float'

Even though the number entered here is equal to the integer value 3, the Python interpreter will still consider it a floating-point value and raise a TypeError.

The simplest way to resolve this issue is by converting the floating-point to an integer and then multiplying it with a string, as shown below.

text = "ItsMyCode"

# convert floating-point value to int
n = int(3.0)
print(text*n)

Output

ItsMyCodeItsMyCodeItsMyCode

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

Now we know that TypeError: can’t multiply sequence by non-int of type float is caused by multiplying a string with a floating-point number. Let us see how we can resolve this error with an example.

Usually, we get this error when we receive input from the user, and it will be of a string format. Consider we have to provide a discount based on the total order value to users.

In the below program, we accept the order value as a string, and we have a fixed discount of 5% on the total order value. 

When we multiply the order_value in string format with a discount value in float, we get an error “can’t multiply sequence by non-int of type float”.

order_value = input("Enter the order value ")
discount = 0.05

total_cost = order_value - (order_value * discount)
print(round(total_cost, 2))

Output

Enter the order value 200
Traceback (most recent call last):
  File "C:\Personal\IJS\Code\main.py", line 4, in <module>
    total_cost = order_value - (order_value * discount)
TypeError: can't multiply sequence by non-int of type 'float'

The best way to resolve this error is to convert the user input string to a floating-point using the float() method. 

This allows us to multiply the order_value and discount because both are floating-point numbers.

order_value = float(input("Enter the order value "))
discount = 0.05

total_cost = order_value - (order_value * discount)
print(round(total_cost, 2))

Output

Enter the order value 200
190.0

Conclusion

We cannot multiply strings with floating-point numbers. We cannot perform this because multiplying strings with integers will create a repetitive sequence of strings.

The same is not possible using the floating-point number as it would result in multiplying a string with decimal point values.

In order to solve this issue, TypeError: can’t multiply sequence by non-int of type ‘float’ ensure that either you are performing a multiplication between string and integer or alternatively you can convert all the string values into a floating-point number before performing any calculations. 

Leave a Reply

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

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Python Dir()

Python dir()

Table of Contents Hide dir() Syntax dir() Parametersdir() Return ValueExample 1: How dir() works?Example 2: When no parameters are passed to dir() method with and without importing external libraries.Example 3: When a module…
View Post
Numpy.repeat() Function

numpy.repeat() Function

Table of Contents Hide SyntaxRepeat ParametersReturn ValueExample 1: Repeating the Single-Dimensional NumPy ArrayExample 2: Repeating the Two-Dimensional NumPy ArrayExample 3: Repeating the NumPy Array Row WiseExample 4: Repeating the NumPy…
View Post