[Solved] TypeError: Can’t Multiply Sequence by non-int of Type ‘float’ In Python?

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

Have you ever come across this stupid bug while coding: typeerror: can't multiply sequence by non-int of type 'float'? I can really feel your frustration when you encounter such bugs. That’s why I am going to make sure that you can resolve this error after reading this article.

Problem Formulation: Fixing typeerror: can't multiply sequence by non-int of type 'float' in Python.

💡 Example 1

Aim: The following program accepts the required user inputs to calculate the BRM and BMRF values.

Output:

Reason of Error:

You can multiply strings in Python with integers to generate a repeating sequence, as shown below.

Output:

*****

However, you cannot multiply float objects with strings in Python. This leads to the occurrence of TypeError: can't multiply sequence by non-int of type 'float'. Let us have a look at an example to verify this.

Output:

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

Please refer to this article to go through the causes of TypeError in Python.

If you have a look at the above example, you will notice that we are trying to calculate the bmrm value by multiplying a floating-point value with a string value. This is because input() function returns a string even if the user enters a number. Thus when a string is multiplied by a floating value it leads to the occurrence of TypeError: can’t multiply sequence by non-int of type ‘float’ in Python.

🖊 Solution: Convert String Input To Floating-Point Value

You can avoid the above TypeError by accepting a user input in the form of a floating-point value instead of a string.

Read User Input as Float in Python

You cannot take user input as a float directly. However, input string can be converted into a float value by using the float() function that returns a float value.

Consider the example given below:

Output:

Enter a decimal number: 2.5
62.5

Now, let us implement the above concept and get rid of the bug in our program.

🖊 The Solution:

Output:

How much do you weigh?:64
How old are you?:26
Next I will need your height. Answer as a decimal. ie:5.8 to represent 5 feet 8 inches. How tall are you?:5.8
BMRM: 795.0
BMRF: 567.64

💡 Example 2

Practice makes a man perfect!👨‍🔬 Hence, before we wrap up our discussion, let us have a look at another example to ensure you have a clear understanding of how to overcome TypeError: can't multiply sequence by non-int of type 'float'.

Problem: Write a program that takes an argument, that must be a numerical value (float or int). It should return a number that equals 3.14 times the entered value.

For example:

  • When user input is “Hello”.
    • Output:- None
  • When user input is 25.25.
    • Output:- 79.285

Your Code:

Output:

Traceback (most recent call last):
File “D:/PycharmProjects/pythonProject1/demo.py”, line 2, in
print(num * 3.14)
TypeError: can’t multiply sequence by non-int of type ‘float’

🖊 Solution:

The obvious reason that lead to the occurrence of TypeError in your code was line 2 wherein you tried to multiply a string input with a floating point value.

  1. To avoid the error you must ensure that the user input is a float value.
  2. Secondly, you have to check if the type of value is a string or a numerical value.

Output:

Enter a decimal number: 25.25
79.285

Explanation

  • When the user input is a number, the try block gets executed. Here, the user input num is type-casted to a floating-point value and multiplied by 3.14.
  • When user input is a string, the except block gets executed.

💡 Example 3

Until now you have seen that TypeError: can't multiply sequence by non-int of type 'float' occurs while multiplying a string with a float value. However, this error will appear every-time you try to multiply any sequential data-type (string, tuple or lists) and a floating point value.

Example:

Output:

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

🖊 Solution:

To avoid the error you have to convert the float value to an integer value as shown below.

Output:

[2, 4, 6, 8, 2, 4, 6, 8]
(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)

Conclusion

Thus in this article we learned that TypeError: can't multiply sequence by non-int of type 'float' occurs when you try to multiply a string (list or tuple) and a floating-point value together. To solve this error you must ensure that the string values are converted to floating-point values before performing the multiplication.

I hope this article helped you. Please subscribe and stay tuned for more exciting articles in the future. Happy learning! 📚

Related Posts

  • Convert String to Boolean in Python
    20 June

    Convert String to Boolean in Python

    Table of ContentsUsing the bool() method to convert string to Boolean in Python.Using the distutils.util.strtobool() method to convert a string to Boolean in Python.Using list comprehension to convert a string to Boolean in Python.Using the map() function along with Lambda function to convert a string to Boolean in Python.Using the JSON parser to convert a […]

  • Convert float to int in Python
    19 June

    Convert float to int in Python

    Table of ContentsUsing the int() function to convert float to int in PythonUsing the math module functions to convert float to int in PythonUsing ceil()Using trunc()Using floor Python provides several in-built functions and modules for datatype conversions. Floating-point numbers can be easily converted to integers using some of these in-built functions. This tutorial will discuss […]

  • Detect keypress in Python
    12 June

    Detect keypress in Python

    Table of ContentsUsing the keyboard module to detect keypress in PythonUsing the pynput module to detect keypress in PythonUsing the msvcrt module to detect keypress in Python Python allows us to work with user input in its programs. We can also work with hardware devices in Python. In this article, we will discuss how to […]

  • Copy a dictionary in Python
    12 June

    7 ways to copy a dictionary in python

    Table of ContentsUsing the copy() function to copy a dictionary in PythonUsing the deepcopy() function to copy a dictionary in PythonUsing the dict() function to copy a dictionary in PythonUsing the unpack operator ** to copy a dictionary in PythonUsing the update() function to copy a dictionary in PythonUsing the dictionary comprehension method to copy […]

  • Escape quotes in Python
    11 June

    Escape quotes in Python

    Table of ContentsUsing \ to escape quotes in PythonUsing \ with replace() to escape quotes in PythonUsing json.dumps to escape quotes in PythonUsing triple single or double quotes to escape quotes in pythonUse r to include \ and quotes in String 💡 Outline You can use \ to escape quotes in Python. If you want […]

  • Sleep for milliseconds in python
    31 May

    How to sleep for milliseconds(ms) in Python

    Table of ContentsUse time.sleep() function to make the program sleep for millisecondsUse threading.Timer() function to make the program sleep for milliseconds 💡 Outline You can use time.sleep() method to sleep for milliseconds in Python. If you want to delay python program by 500 ms, then you need to pass 0.5 as parameter to sleep method. […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.