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

Posted in /  

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

Vinay Khatri
Last updated on May 30, 2024

    Python support three sequential data types, namely a string , list , and tuple . All these three data types store their elements in sequential order using indexing. And we can use the index values to access the individual elements or slices of elements from these data types. But if we multiply any of these sequential data objects with a floating-point value or object, we will receive the TypeError: can't multiply sequence by non-int of type 'float' Error.

    In this Python guide, we will discuss what this error means, why it occurs in the Python program, and debug it. We will also use some examples for a better understanding of this error. So without further ado, let's get started with the Error statement.

    Python Error: TypeError: can't multiply sequence by non-int of type 'float'

    Let's have a look at the error statement first. We can see that the complete error statement is divided into two parts Error Type ( TypeError ) and Error Message ( can't multiply sequence by non-int of type 'float' ) if we look carefully.

    1. Error Type ( TypeError ): This is the Error's type, and it generally occurs when we perform some invalid or incorrect operation on a Python object type.
    2. Error Message ( can't multiply sequence by non-int of type 'float' ): This is the actual error message, telling us that we are using a multiplication operation between a sequential object and a float number, which is invalid. Because Python only supports the multiplication operation between the integer object and sequential object.

    Error Reason

    Python supports a unique feature in which if we multiply a sequential object like string, tuple, and list with an integer value, and in return, we get a new string, tuple, and list, repeated N number of times (where N is the value of integer).

    Example

    string = "abc"
    list_ = ['a', 'b', 'c']
    tuple_ = ('a', 'b', 'c')
    
    # repeate ate string 2 times
    string = string*2
    
    # repeate list 3 times
    list_ = list_*3
    
    # repeate tuple 4 times
    tuple_ = tuple_*4
    
    print("string: ",string)
    print("list_:",list_)
    print("tuple_:",tuple_)

    Output

    string: abcabc
    list_: ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']
    tuple_: ('a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c')

    But if we multiply a list, string, or tuple with a floating-point number, we will encounter the TypeError: can’t multiply sequence by non-int of type 'float' error.

    Example

    string = "abc"
    
    # repeate ate string 2 times using float
    string = string*2.0
    
    print(string)

    Output

    Traceback (most recent call last):
    File "main.py", line 4, in <module>
    string = string*2.0
    TypeError: can't multiply sequence by non-int of type 'float'

    If we read the error message carefully, we can easily guess that Python can not multiply a sequence like string, tuple, or list with a floating-point number. It only supports int (integer).

    Common Scenario

    The most common place where many Python learners commit this error is when they ask the user to enter a numeric value and forget to convert it into a float or integer. And multiply the user input string value with a floating-point number.

    Example

    # perimeter of the circle
    radius = input("Enter the radius of the circle: ")
    
    # float number
    PI = 3.14159265358
    
    perimeter = 2 * PI * radius
    
    print("perimeter of the circle is: ", round(perimeter,2) )

    Output

    Enter the radius of the circle: 34
    Traceback (most recent call last):
    File "main.py", line 7, in <module>
    perimeter = 2 * PI * radius
    TypeError: can't multiply sequence by non-int of type 'float'

    Break the code

    In the above program, we are getting the error at line 7 because there Python could not multiply the float number PI with the radius string value 34 . Whenever we input data from users using the input() method, we receive that error in string type. And we know when we multiply a floating-point number like PI with a string value like radius we receive the TypeError: can't multiply sequence by non-int of type 'float' error.

    Solution

    To solve the above problem, we need to convert the user entered radius into a floating-point or integer number, so we can get the correct output. To do that, we can use the Python inbuilt float() function.

    Example Solution

    # perimeter of the circle
    # convert the input value into float
    radius = float(input("Enter the radius of the circle: "))
    
    # float number
    PI = 3.14159265358
    
    perimeter = 2 * PI * radius
    
    print("perimeter of the circle is: ", round(perimeter,2) )

    Output

    Enter the radius of the circle: 324
    perimeter of the circle is: 2035.75

    Wrapping Up!

    In this Python tutorial, we learned about Python “typeerror: can’t multiply sequence by non-int of type ‘float’” Error. This error occurs in Python when we try to multiply a list, tuple, or string object with a floating-point number. Although Python supports multiplication operations between a sequential object and an integer, it is only used to repeat the number of elements present in that sequential object.

    If you ever encounter this error in your Python program, just look for the error line code and fix the problem where you are multiplying your sequential object with the floating number. If you are still getting this error in your program, please share your code in the comment section, and we will try to help you in debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments