So you have been presented with the TypeError: ‘float’ object is not callable in Python, but unsure how to fix? Well read on so we can get you a solution.
First things an overview of a Float
A float is any number that can contain a decimal place separating an integer from the fractions to the right of the decimal point.
As an example, in the below code, we have the exact same number. But when a computer comes to read it, it treats them differently, due to the fact that b has a decimal point and a fraction to the right of it.
a = 1
b = 1.0
print (type(a))
print (type(b))
Output:
<class 'int'>
<class 'float'>
So how in a piece of code would this error occur?
In the below code, the error occurs. The reason behind this is that float is an actual function that changes a value to a float data type.
As a result, you cannot assign a variable name as “float” and then call it separately in its code. Python specifically does not allow a programmer to use a function as a variable name.
a= float(10)
b= float(11)
c= float(12)
float = a
d = float(a/b*c)
print(d)
Output:
d = float(a/b*c)
TypeError: 'float' object is not callable
So how is this fixed?
In order to fix this, we rename the variable “float” to something else, and make sure that the new name is not another function name!
As can be seen when we do this, the below output runs as expected with no errors:
a= float(10) b= float(11) c= float(12) float_value = a ===> This line was corrected from above from "float" to "float_value" d = float(a/b*c) print(d) Output: 10.909090909090908
In summary the crux of this problem is that when creating a variable, it cannot be a function name.
You can see similar posts here on TypeErrors.
Have you seen these posts?