How to Fix TypeError: Can’t Multiply Sequence by non-int of Type ‘float’ In Python?

✯ Overview

Problem: Fixing TypeError: Can’t Multiply Sequence by non-int of Type ‘float’ in Python.

Example: Consider that you want to calculate the circumference of a circle using the radius entered by the user, as shown below.

As you can see above, we encountered a TypeError while executing our code.

Bugs like these can be really frustrating!? But, after you finish reading this article, these silly bugs will no longer be a matter of concern for you. Therefore, to understand what causes such errors and how to avoid them, it is important to answer a few questions:

  • What is a TypeError in Python?
  • Why does Python raise TypeError: Can’t Multiply Sequence by non-int of Type ‘float’ ?
  • How do we fix TypeError: Can’t Multiply Sequence by non-int of Type ‘float’ ?

Let’s dive into each question one by one.

✯ What is TypeError in Python?

Python raises a TypeError when you try to use a function or call an operator on something that is of the incorrect type.

Example:

print('Python'+3.8)

Output:

Traceback (most recent call last):
  File "D:/PycharmProjects/PythonErrors/TypeError Can’t Multiply Sequence by non-int of Type ‘float’ In Python.py", line 1, in <module>
    print('Python'+3.8)
TypeError: can only concatenate str (not "float") to str

⮞ We encountered the above error because Python expects the + operator between two numeric types. However, in the above example we tried to add a string and a float value. Thus, Python throws a TypeError, saying us that one of the parameters was of incorrect type.

This brings us to the next question!

✯ Why does Python raise – ‘TypeError: can only concatenate str (not “float”) to str‘ ?

Python allows multiplication of a string and a float value. This means it generates a repeating sequence of the string such that the given string value is repeated as many times as mentioned as the integer value.

Example:

print('Finxter '*5)

Output:

Finxter Finxter Finxter Finxter Finxter

? But, can you multiply a floating value and a string value in Python?

Answer: No, you cannot multiply a string value and a float value in Python. You will get TypeError: can't multiply sequence by non-int of type 'float' if you try to do so.

Example:

radius = input("Enter the radius: ")  # string input
print('Circumference = ', 2 * 3.14 * radius)

Output:

Enter the radius: 5
Traceback (most recent call last):
  File "D:/PycharmProjects/PythonErrors/TypeError Can’t Multiply Sequence by non-int of Type ‘float’ In Python.py", line 2, in <module>
    print('Circumference = ',2*3.14*radius)
TypeError: can't multiply sequence by non-int of type 'float'

Solution: Please go through this solution only after you have gone through the scenarios mentioned below.

radius = float(input("Enter the radius: "))
print('Circumference = ', 2 * 3.14 * radius)
# OUTPUT:
 # Enter the radius: 5
 # Circumference =  31.400000000000002

➥ Similarly, whenever you try to multiply a float value and a sequential data-type (string/tuple/lists), Python will raise a TypeError: can’t multiply sequence by non-int of type ‘float’.

Example:

my_list = [100, 200, 300, 400]  # list
my_tup = (9, 99, 999, 9999, 99999)  # tuple
print(2.0 * my_list)
print(2.0 * my_tup)

# DESIRED OUTPUT:
# [100, 200, 300, 400, 100, 200, 300, 400]
# (9, 99, 999, 9999, 99999, 9, 99, 999, 9999, 99999)

Output:

Traceback (most recent call last):
  File "D:/PycharmProjects/PythonErrors/TypeError Can’t Multiply Sequence by non-int of Type ‘float’ In Python.py", line 3, in <module>
    print(2.0 * my_list)
TypeError: can't multiply sequence by non-int of type 'float'

✯ How to Fix – TypeError: Can’t Multiply Sequence by non-int of Type ‘float’ ?

? To avoid the occurrence of TypeError: Can’t Multiply Sequence by non-int of Type ‘float’ you must ensure that the user input is a floatingpoint value by typecasting the string input to float.

Let us have a look at the solutions to this error with the help of numerous scenarios.

⮞ Type 1: Convert Temperature Fahrenheit To Celsius

Problem: given temperature in Fahrenheit; how to convert it to Celsius?

Our Code:

Celsius = input("Enter the temperature: ")
print(Celsius, "°C = ", Celsius * 1.8 + 32, "°F")

Output:

Traceback (most recent call last):
  File "D:/PycharmProjects/PythonErrors/TypeError Can’t Multiply Sequence by non-int of Type ‘float’ In Python.py", line 2, in <module>
    print(Celsius, "°C = ", Celsius * 1.8 + 32, "°F")
TypeError: can't multiply sequence by non-int of type 'float'

Solution:

Celsius = float(input("Enter the temperature: "))
print(Celsius, "°C = ", Celsius * 1.8 + 32, "°F")

Output:

Enter the temperature: 37
37.0 °C =  98.60000000000001 °F

Voila! we have successfully resolved our problem. ?

⮞ Type 2:  Multiplying Any Sequential Data-Type (string, tuple or lists) And a Floating-Point Value

Python raises TypeError: can't multiply sequence by non-int of type 'float' every time a sequential data is multiplied by a floating-point value.

Example 1:

li = ['a', 'b', 'c']  # list
tup = (100, 200, 300)  # tuple
print(2.0 * li)
print(2.0 * tup)

# DESIRED OUTPUT:
# ['a', 'b', 'c', 'a', 'b', 'c']
# (100, 200, 300,100, 200, 300)

Output:

C:\Users\DELL\AppData\Local\Programs\Python\Python38\python.exe "D:/PycharmProjects/PythonErrors/TypeError Can’t Multiply Sequence by non-int of Type ‘float’ In Python.py"
Traceback (most recent call last):
  File "D:/PycharmProjects/PythonErrors/TypeError Can’t Multiply Sequence by non-int of Type ‘float’ In Python.py", line 3, in <module>
    print(2.0 * li)
TypeError: can't multiply sequence by non-int of type 'float'

Solution: The error can be avoided by type-casting the float value i.e., 2.0 to an integer value i.e. 2.

li = ['a', 'b', 'c']  # list
tup = (100, 200, 300)  # tuple
print(int(2.0) * li)
print(int(2.0) * tup)

Output:

['a', 'b', 'c', 'a', 'b', 'c']
(100, 200, 300, 100, 200, 300)

Example 2: Let us try and calculate the total tax @10% from a given list of prices.

price = [100, 200, 300]
print('Tax = $%.2f' % sum(price) * 0.1)

# DESIRED OUTPUT:
# Tax = $60.00

Output:

Traceback (most recent call last):
  File "D:/PycharmProjects/PythonErrors/TypeError Can’t Multiply Sequence by non-int of Type ‘float’ In Python.py", line 2, in <module>
    print('Tax = $%f' % sum(price) * 0.1)
TypeError: can't multiply sequence by non-int of type 'float'

Solution:

Since you have the % operator in your expression, the call to sum() is tied to the string, and effectively you have:

<string_value> * tax

The solution lies in adding proper parentheses to force the precedence you want as shown below.

price = [100, 200, 300]
print('Tax = $%.2f' % (sum(price) * 0.1))

Output:

Tax = $60.00

Conclusion

I hope you found this article helpful. Please subscribe and stay tuned for more interesting articles in the future. Happy learning! ?

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!