[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.

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

💡 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

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.