Sometimes, working with functions and calling them, you might encounter TypeError:” float object is not callable” error. This error is due to the fact that the floating object cannot be used as callable which means we are treating the floating object as a function and calling it which is not allowed in Python. In this short article, we will discuss how we can solve TypeError:” float object is not callable” error and will discuss the reasons for getting such an error. Moreover, we will also learn how to interpret and understand errors in Python.

How to solve TypeError: ‘float’ object is not callable error in Python
The TypeError: ‘float’ object is not callable error in Python occurs when we are dealing with functions and we treat a floating object as a function and call it. In Python, only the methods and functions are callable and when we tried to call any other data type, we will get the error.
There can be many reasons for having TypeError: ‘float’ object is not callable error. The two most commons reasons are:
- We are calling a floating object and treating it as a function
- Our function and floating variable have the same name
Let us take the examples for both of the cases. Let us say we have a variable and we are calling it:
# creating a variable
var = 3.23
# calling the var
var()
Output:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_40779/3359193321.py in <module>
3
4 # calling the var
----> 5 var()
TypeError: 'float' object is not callable
The second reason might be you have the same name for the variable and function as shown below:
# creating a function
def main():
return 3.43
# calling a function
main = main()
# printing
print("The number is:", main())
Output:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_40779/217479463.py in <module>
7
8 # printing
----> 9 print("The number is:", main())
TypeError: 'float' object is not callable
The reason for getting this error is that the name of the function and variable is the same.
The error can be solved by using any of the following methods:
- Changing the name of the variable
- Using type() method
- Missing operation
- Using try-except block
Solution-1: Changing the name of the variable
If you are getting TypeError: ‘float’ object is not callable error because of having the same name for the variable and the function, then you can change the name of the variable. See the example below:
# creating a function
def main():
return 3.43
# calling a function
var = main()
# printing
print("The number is:", main())
Output:
The number is: 3.43
As you can see, we got the floating number as an output.
Solution-2: Use the type() method to find the type
Another way is to use the type() method to find the type of the object. If the object is a floating type, then don’t call it.
# creating a function
def main():
return 3.43
# calling a function
var = main()
# printing
print("The type is:", type(main))
print("The type is:", type(var))
Output:
The type is: <class 'function'>
The type is: <class 'float'>
Notice that the type of the main() is function while the type of the var is float.
Solution-3: Missing operation
Sometimes, the error also occurs when you want to perform some mathematical operations but forget the operation. Let us take an example and see:
a = 11.21
b = 32
c = 1
# error
result = a (b + c)
Output:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_40779/1386005936.py in <module>
4
5 # error
----> 6 result = a (b + c)
TypeError: 'float' object is not callable
In such a case, you need to debug your code and find out which operation is missing, so that you can put in the correct operation and the problem will solve.
a = 11.21
b = 32
c = 1
# error
result = a - (b + c)
Notice that we added the subtraction operation.
Solution-4: Try-except block
If you are not sure what kind of data type you will get, then you can always use the try-except block in order to handle if any error occurs.
The try-except block in Python is the main weapon to handle any kind of error. The code inside the try block will be executed first. If any error occurs, then instead of crashing the script, the except block will be executed. See the example below:
# variable
var = 4.323
try:
print(var())
except:
print("there is an error in try block")
Output:
there is an error in try block
As you can see, instead of getting TypeError: ‘float’ object is not callable error, we got a message saying that there is a problem in our code.
Understand TypeError: ‘float’ object is not callable error
Let us now understand errors in Python taking TypeError: ‘float’ object is not callable as an example. In Python, errors usually have two main parts. The first part of the error shows the category of the error which in our case is the TypeError. The type error means there is something wrong with the data type and we are using it in correctly.
The second part of the error helps us to figure out what kind of type error is there. In our case, it clearly says that we are trying to call a floating object a function.
The following are some of the common reasons for getting this error:
- We are trying to use a floating variable as a function
- The function and floating variable have the same name
- We forget to put the mathematical operation
Summary
In this short article, we discussed how we can solve TypeError: ‘float’ object is not callable error using various methods. In general, we discussed 4 different methods to solve the error. Moreover, we also discussed the reasons for getting this error.
Related Issues:
- TypeError: this.getOptions is not a function[Solved]
- Consider using the –user option or check the permissions
- [Solved] AttributeError: module ‘matplotlib.cbook’ has no attribute ‘iterable’