Python TypeError: ‘float’ object is not callable Solution

Posted in /  

Python TypeError: ‘float’ object is not callable Solution
vinaykhatri

Vinay Khatri
Last updated on December 25, 2024

    Python supports a distinct data type to store floating points or decimal numbers, and that data type is known as Python float. Floating-point values are the numbers with decimal values, and Float is their data type.

    Floating-point values are like other data types present in Python, but they represent decimal numerical values. But if we treat them as a function and call them using parenthesis, we get the TypeError: ‘float’ object is not callable Error.

    In this Python tutorial, we will discuss this Python error and learn why it raises and how to solve it. We will also discuss some Python code snippet that causes this error and solve them so that you can have a better understanding of this error. So let's get started with the error itself.

    Python Error: TypeError: ‘float’ object is not callable

    The Python Error TypeError: 'float' object is not callable is divided into two statements, Error Type and Error Message, separated with a colon : .

    1. Error Type ( TypeError ): TypeError is one of the most common Python standard exceptions, and it raises when we perform an incorrect operation on a Python object.
    2. Error Message( 'float' object is not callable ): This is the error message, which tells us that we are calling a Python float object as a function, which is invalid in Python.

    Error Reason

    Float objects are used in Python to store floating-point numbers, but if we call a float variable as a function by putting a parenthesis after its variable name, we receive the TypeError: ‘float’ object is not callable error.

    Example

    # a floating point number
    my_num = 300.23
    
    # call the float number as a function
    my_num()

    Output

    Traceback (most recent call last):
    File "main.py", line 5, in <module>
    my_num()
    TypeError: 'float' object is not callable

    Break the Code

    In the above example, we are getting the error because when we put the parenthesis () after a variable name, Python treats it as a function calling statement. But in the above example, my_num it is not a function. It is a float number. That's why Python threw the error 'float' object is not callable , which simply means we can not call the float objects functions.

    Common Error Example

    There are two common major cases when many new Python learners commit the mistake and encounter this error.

    • Scenario 1: Used float as a variable name and used the float() function afterward.
    • Scenario 2: Forget to put the math operator between the opening parenthesis and the float number.

    Scenario 1 (Used float as a variable name)

    The most common mistake that many new python learners do is when they use the float keywords as a variable name to store a floating-point number, and in the same program, they also use the float() function to convert an object to a floating-point object.

    Example

    # define a variable by name float
    float = 12.0
    
    height = float(input("Enter your height in inches: "))
    foot = height/float
    print(f"Your height is: {round(foot,2)} Feet")

    Output

    Enter your height in inches: 56.4
    Traceback (most recent call last):
    File "main.py", line 4, in <module>
    height = float(input("Enter your height in inches: "))
    TypeError: 'float' object is not callable

    Break the Code

    In the above example, we are trying to convert the user entered height in inches to feet. But we are receiving the TypeError: 'float' object is not callable error at line 4.

    This is because, in line 2, we have defined a variable by name float whose value is 12.0 , that represents the value to convert the inches to feet. But, in line 4, we are using the Python float() function to convert the user input height to a floating-point number.

    But now for Python float is not a function anymore. It is a floating-point variable whose value is 12.0. that is defined in line 2. By which it will not call the actual Python inbuilt function float(). Instead, it will call the float variable a function, which will lead to the TypeError: 'float' object is not callable error.

    Solution

    The solution for the above scenario is very simple. All we need to do is change the name of the float variable to something else. This is also very important. While we want to write good code in Python, we never use keywords and function names to define a variable.

    Solution 1

    # define a variable by name inch
    inch = 12.0
    
    height = float(input("Enter your height in inches: "))
    foot = height/inch
    print(f"Your height is: {round(foot,2)} Feet")

    Output

    Enter your height in inches: 67.4
    Your height is: 5.62 Feet

    Scenario 2 (Forget to put the math operator )

    In mathematics, if we do not put any operator between the number and the opening parenthesis (, then we treat that expression as a multiplication symbol between the number outside the parenthesis and the number inside the parenthesis.

    For instance(in mathematics)

    2.0(3.0+4.0) = 14.0

    But in Python programming, we need to specify the Arithmetic operator between the number and the opening or closing parenthesis; else, we get the error.

    for instance (in python)

    2.0 (3.0 +4.0)  = error

    Example

    #floating point numberss
    a= 2.0
    b= 3.0
    c= 4.0
    
    #expression
    result = a(b+c)
    
    print(result)

    Output

    Traceback (most recent call last):
    File "main.py", line 7, in <module>
    result = a(b+c)
    TypeError: 'float' object is not callable

    Break the code

    If we look at the error code statement, we can see that the error occurred on line 7 with the result = a(b+c) statement. This is because we forget to put the * operator after variable a . The Python interpreter mishandles the floating-point variable a with the function calling statement.

    Solution

    The solution to this problem is also very straightforward. All we need to do is place the Arithmetic operator between variable a and ( parenthesis.

    solution 2

    #floating point numbers
    a= 2.0
    b= 3.0
    c= 4.0
    
    #expression
    result = a*(b+c)
    
    print(result)

    Output

    14.0

    Conclusion

    In this Python tutorial, we learned what is TypeError: ‘float’ object is not callable error in Python and how to solve it. If we look closely at the error message, we can tell that the error is related to the float and calling a function. The only reason this error occurs is when we write a function calling statement using a Python floating-point variable or value.

    If you have a basic knowledge of Python floats and functions, debugging this error would be a piece of cake for you. If you are still getting this error in your Python program, you can share your code in the comment section. We will be happy to help you in debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments