[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! 📚

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 29 June

    Get Day Name from Date in Python

    Table of ContentsUse the Calendar Module to Get the Name of the Day in PythonUse the strftime() Method to Get the Name of the Day in PythonUse Pandas Timestamp Method to Get the Name of the Day in PythonUse the weekday() Method to Get the Name of the Day in PythonUse the isoweekday() Method to […]

  • 27 June

    Fill Array with Random Numbers in Python

    Table of ContentsFill array with random numbers in PythonWays to fill array with random numbers in PythonUsing the numpy.random.randint() function to fill array with random numbers in PythonUsing the numpy.random.Generator.integers() function to fill array with random numbers in PythonUsing the random.randint() function to fill array with random numbers in PythonConclusionWas this post helpful? Fill array […]

  • 25 June

    Convert Roman Number to Integer in Python

    Table of ContentsHow to Convert Roman Number to Integer in PythonHow to convert roman number to integer in python?Using the if...else statement to convert roman number to integer in Python.Using classes and a dictionary to convert roman number to integer in Python.Using the roman module to convert roman number to integer in Python.ConclusionWas this post […]

  • 11 June

    Remove Single Quotes from String in Python

    Table of ContentsRemove single quotes from string in Python.How to remove single quotes from string in Python?Using the replace() function to remove single quotes from string in Python.Using a for loop to remove single quotes from string in Python.Using the join() function to remove single quotes from string in Python.Using the re module functions to […]

  • 11 June

    Escape Backslash Character in Python

    Table of ContentsEscape backslash character in python.How to use escape backslash character in python?Using the escape backslash character in python to represent whitespace characters.Using the escape backslash character in python to turn special characters into ordinary characters.Using the escape backslash character in f-strings in Python.Using the escape backslash character in raw strings in Python.Was this […]

  • 11 June

    Break out of function in Python

    Table of ContentsBreak out of function in PythonHow to break out of function in Python?Using the return statement to break out of function in PythonUsing the try and except block to break out of function in PythonUsing the sys.exit() function to break out of function in PythonConclusionWas this post helpful? In this post, we will […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.