TypeError 'float' object is not callable
When working with different functions, there may be a situation where the function is not properly called or invoked. You might encounter an error called “TypeError 'float' object is not callable”. This may be due to the calling of a float variable or object that is not callable.
This may be due to syntax error or incorrect function definition. In this article, we will look at this error and its solution in detail.
Consider the following piece of code as an example:
# Declare functon with name "areaasquare"
def areaasquare(l,b):
asquare = l*b
return asquare
# Taking value of function "areaasquare"
# In variable name "areaasquare"
areaasquare = areaasquare(5.4,6)
print('Area of Square: ',areaasquare)
areaasquare = areaasquare(33,3.2)
print('Area of Square: ',areaasquare)
Output:
File "pyprogram.py", line 7, in <module>
areaasquare = areaasquare(33,3.2)
TypeError: 'float' object is not callable
This error is caused due to a problem in the function definition. While trying to calculate the area of the square, it overrides the function definition. This happens in these lines:
areaasquare = areaasquare(33,3.2)
This can be fixed by changing the name of the function to calculate_areaasquare, the main function definition is not affected and the error can be avoided.
Solution:
# Declare functon with name "calculate_areaasquare"
def calculate_areaasquare(l,b):
asquare = l*b
return asquare
# Taking value of function "areaasquare"
# In variable name "areaasquare"
areaasquare = calculate_areaasquare(5.4,6)
print('Area of Square: ',areaasquare)
areaasquare = calculate_areaasquare(33,3.2)
print('Area of Square: ',areaasquare)
Output:
Area of Square: 32.400000000000006
Area of Square: 105.60000000000001