[Solved] TypeError: ‘float’ object is not callable

TypeError: ‘float’ object is not callable is a general error message that can arise while performing any operation on the float object. There are multiple reasons that can result in this error. In this article, we will study different scenarios that can result in this error message. Stay tuned and keep reading the article. Now don’t waste your time and let’s begin.

'float' object is not callable

What is TypeError: ‘float’ object is not callable?

We will break the error message into three sections as explained below to understand it better.

TypeError: TypeError is a standard Python exception. It raises when we perform an operation on an object which is not supported by that type of object.

float: It indicates that there is an issue while performing float type of object in a code.

object is not callable: It tells us that we cannot call float variable as a function. It’s a variable that is used to store a value. We cannot call this variable as a function.

What are callable and non-callable objects in Python?

Callable objects: Objects which can be called and return something or perform any operation are called callable objects. Functions, methods, or classes are callable objects.

Non-Callable objects: Objects which cannot be called and return something or perform any operation are called non-callable objects. Python variables are Non-Callable objects such as int, float, etc.

Why you are getting TypeError: ‘float’ object is not callable?

You are getting this error message because in your code you are trying to treat a float variable as a function. Float is a standard python variable you cannot call it a function.

There are multiple reasons or scenarios that could result in this error message. Such as missing mathematical operator while performing a calculation or function name and variable names are same or calling float variable as a function.

How to resolve TypeError: ‘float’ object is not callable?

There are following three major scenarios that result in the given error message. Here we will study in detail these three potential scenarios with examples.

1. Calling a floating-point variable as a function

Below is the sample program to calculate the area of a circle. CircleArea is a flow type of object which stores a floating-point value of a calculated area.

# File: Sample_Program_1.py
# Sample Python program to calculate area of a circle

# get circle radius from a user as a argument
radius = float(input("Please enter the radius of a circle: "))

# Calculate area of a circle using mathematical formula.
circleArea = 3.14 * radius *radius

print ("Area of circle is: " + str(circleArea()))

When we execute the above program, it will give the following error message.

C:\Sample_Program>python Sample_Program_1.py
Please enter the radius of a circle: 25
Traceback (most recent call last):
File “C:\Sample_Program\Sample_Program_1.py”, line 10, in
print (“Area of circle is: ” + str(circleArea()))
TypeError: ‘float’ object is not callable

At line number 10 it’s throwing an error message because we are calling float variable circleArea as a function by adding parenthesis. But circleArea is a float type of object and we cannot call it as a function. Calling circleArea as a function is an invalid operation hence we are getting the above error message.

Solution:

This error can be easily solved by removing parenthesis from the below line of the circleArea as shown below.

print ("Area of circle is: " + str(circleArea))

When we make the above changes in the code and execute the program, it will give the following output.

C:\Sample_Program>python Sample_Program_1.py
Please enter the radius of a circle: 25
Area of circle is: 1962.5

2. Missing arithmetic operator while performing the calculations:

To understand this scenario, please refer to the below example to calculate the area of an eclipse.

# File: Calculate_Area_Of_Eclipse.py
# Sample Python program to calculate area of an eclipse

# import math header file
import math

# Function to calculate area of an eclipse
def areaOfEclipse(a,b):
    eclipseArea = math.pi(a*b)
    return eclipseArea

#Get major and minor axes eclipse values from the user
majorAxis = float(input("Please enter semi-major axis of eclipse: " ))
minorAxis = float(input("Please enter semi-minor axis of eclipse: "))

# Print the calculated eclipse area
print("Area of an eclipse is: " + str(areaOfEclipse(majorAxis,minorAxis)))

When we execute the above program, it will give the following error message.

C:\Sample_Program>python Calculate_Area_Of_Eclipse.py
Please enter semi-major axis of eclipse: 14.5
Please enter semi-minor axis of eclipse: 13
Traceback (most recent call last):
File “C:\Sample_Program\Calculate_Area_Of_Eclipse.py”, line 17, in
print(“Area of an eclipse is: ” + str(areaOfEclipse(majorAxis,minorAxis)))
File “C:\Sample_Program\Calculate_Area_Of_Eclipse.py”, line 9, in areaOfEclipse
eclipseArea = math.pi(a*b)
TypeError: ‘float’ object is not callable

As clearly mentioned in the error message, the above code is failing at eclipseArea = math.pi(a*b) line of code. In general mathematics value inside the parenthesis gets multiplied by to value available outside the parenthesis. For example, if you perform 2(3) in mathematics it will give a result of 6. It considers 2(3) as a 2*3.

But in Python parenthesis means function call. Hence it’s throwing the error message.

Solution:

The solution for this scenario is very simple. Just use * between math.pi and (a*b) as shown below.

eclipseArea = math.pi*(a*b)

When we make the above changes in the code and execute the program again then it will give the following result.

C:\Sample_Program>python Calculate_Area_Of_Eclipse.py
Please enter semi-major axis of eclipse: 14.5
Please enter semi-minor axis of eclipse: 13
Area of an eclipse is: 592.190215201676

3. The Same function name and variable name

In your python program if the variable name and function name are the same then it may result in the ‘float’ object is not callable error message.

To understand this scenario, please refer to the below example to calculate area of a rectangle.

# File: Calculate_Area_Of_Rectangle.py
# Sample Python program to calculate area of an rectangle

# Function to calculate area of an rectangle
def areaOfRectangle(h,w):
    rectangleArea = h*w
    return rectangleArea

# Area of rectangle 1
areaOfRectangle = areaOfRectangle(6.5,7.8)

print("Area of a rectangle is: " + str(areaOfRectangle))

# Area of rectangle 2
areaOfRectangle = areaOfRectangle(8.5,9);

print("Area of a rectangle is: " + str(areaOfRectangle))

When the above code is executed it will give the following error message.

C:\Sample_Program>python Calculate_Area_Of_Rectangle.py
Area of a rectangle is: 50.699999999999996
Traceback (most recent call last):
File “C:\Sample_Program\Calculate_Area_Of_Rectangle.py”, line 15, in
areaOfRectangle = areaOfRectangle(8.5,9);
TypeError: ‘float’ object is not callable

In the above example, we are using areaOfRectangle to store the area value, and also the name of a function to calculate the area of a rectangle is also areaOfRectangle. Hence it results in the error message.

Solution:

The solution for this scenario is very simple. Just keep different names for variables and functions as shown below. Here we have changed the function name from areaOfRectangle to areaOfRectangle_func.

# File: Calculate_Area_Of_Rectangle.py
# Sample Python program to calculate area of an rectangle

# Function to calculate area of an rectangle
def areaOfRectangle_func(h,w):
    rectangleArea = h*w
    return rectangleArea

# Area of rectangle 1
areaOfRectangle = areaOfRectangle_func(6.5,7.8)

print("Area of a rectangle is: " + str(areaOfRectangle))

# Area of rectangle 2
areaOfRectangle = areaOfRectangle_func(8.5,9);

print("Area of a rectangle is: " + str(areaOfRectangle))

When the above code is executed, it will give the following result.

C:\Sample_Program>python Calculate_Area_Of_Rectangle.py
Area of a rectangle is: 50.699999999999996
Area of a rectangle is: 76.5

Summary:

In this article, we studied that TypeError: ‘float’ object is not callable can arise because of different reasons and their solutions. We hope that you are able to solve the error message at your end by following the above scenarios. If you are still facing the same error message then please do mention it in the comment section or you can reach out to us using Contact Form or our official mail id i.e. hello.technolads@gmail.com

Further read:

TypeError: ‘builtin_function_or_method’ object is not subscriptable

TypeError: ‘float’ object is not subscriptable

Only integer scalar arrays can be converted to a scalar index

Leave a Comment