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

Battling with any type of error during developing something gives a large amount of frustration. In python, we can encounter many types of error which can be easy to recognize or sometimes it can be very hard to recognize.

So today we are going to see a common type of error in python which is very difficult to get recognized sometimes. Because these errors cannot be recognized with some basic debugger tools because, in the sense of logic, your program will be correct everywhere.

Today we will discuss a common Type error named cannot multiply sequence by non-int of type ‘float’. We will see what are the causes of this error and what can be possible solutions for this error. We will see two different cases of occurring of this error and we will see the approach to find and solve this type of error in our programs.

First, we will see what ‘sequence’ means in this error?

In python, Strings, list, tuple or sets etc. are known as sequence.

When we try to perform some operations using any of the sequences from lists, tuples, strings or sets, and we do some mistakes in performing those operations then we will encounter this type of error.

Now let’s see some cases in which this error can occur.

Reasons for Error

Case 1:

Assume that you are writing a program that calculates the area of the circle. In this case radius of the circle is taken as an input from the user.

You have written the code like this.

When you run your code in the terminal, then you will see the output in the terminal as follows.

Output:

You can see that the error occurs in our program. Now we will see the second case of the error.

Case 2:

Suppose we are writing a program in which we want the output as given below.

[ 1 , 2 , 3 , 4 , 5 , 1 , 2 , 3 , 4 , 5 ]

( ‘a’ , ‘b’ , ‘c’ , ‘a’ , ‘b’ , ‘c’ )

First, we have written our code with preferred input as shown in the example below.

When we try to run this code in our terminal then we will encounter the error as shown below.

Output:

From these cases, we can understand that this error mostly occurs when we try to perform operations between those datatypes which are unsupported in python.

As we can see in case 1, we were trying to perform an operation ‘multiplication’ between float datatype and string datatype. Python does not support this operation between these datatypes so it raises that error.

The same thing happening in Case 2, we are trying to perform an operation between a list and a float datatype. And in the terminal, we got an error because python does not support this operation.

To solve this error, we have to check whether we are trying to perform an operation between any two datatypes which is unsupported in python.

Let’s see the updated solution of previous cases.

Solutions for Error

Case 1:

Output:

And we got the desired solution.

While we are taking inputs from the user, python default takes input as a string, now we convert it into integer so that we can perform operation ‘multiplication’. And by this way, we can solve the error.

Case 2:

In this case, we are trying to multiply the list and tuple with floating point value. And python is not supporting this so it shows the error, to eliminate this error we have to alter our code.

Output:

After Modifying line 3 and 4, we can get our desired output.

In line 3 and 4, we are multiplying the list with integer value, so the operation is performed successfully, we are able to get the desired output from the code.

Conclusion

In python, we may encounter so many errors which can take much longer to find and solve that error, sometimes the error can be logical some times it can be a syntax error. We have to observe carefully to find any type of error, but finding logical errors can take much time. In this article, we have talked about the error called can’t multiply sequence by non-int of type ‘float’.

This error occurs when we are trying to perform any operation between two datatypes in which one of them is Float. That Operation will not be supported by Python.

Whenever we encounter these types of error, first we have to check whether we are trying to perform the operation between unsupported datatypes in python. When we correct this then our code will be executed successfully.

Leave a Comment

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