Floating-point values are not callable. This is because floating points store numerical values. They are not functions that return a particular value when called. If you try to call a floating-point value as if it were a function, you encounter a “TypeError: ‘float’ object is not callable” error.
In this guide, we discuss how this error works and why you may find it in your code. We walk through an example scenario to help you learn how to fix it.
TypeError: ‘float’ object is not callable
A set of parentheses denotes a function call. A function call instructs the contents of a function to run. Only functions can be called. Other values, like floating points, do not return values, and so they cannot be called.
The “TypeError: ‘float’ object is not callable” error happens if you follow a floating point value with parenthesis. This can happen if:
- You have named a variable “float” and try to use the float() function later in your code.
- You forget an operand in a mathematical problem.
Let’s look at both of these potential scenarios in detail.
Scenario #1: Naming a Variable “float”
Let’s write a program that calculates the tips each member of the wait staff at a restaurant are due. The restaurant splits all the tips equally.
We start by asking the user to tell the program how much was received in tips and how many staff members were working on a particular day using the input() method:
staff_working = input("How many staff were working today? ") float = float(input("How much was earned in tips? "))
Next, we write a math equation that calculates how much each member of the wait staff is due in tips:
staff_due = float / float(staff_working) rounded = round(staff_due, 2) print(rounded)
We round the amount that each staff member is due to two decimal places so that we have a monetary value that we can give to each staff member in tips. We print this rounded amount to the console. Next, run our code and see what happens:
How many staff were working today? 7 How much was earned in tips? 300 Traceback (most recent call last): File "main.py", line 5, in <module> rounded = round(staff_due, 2) TypeError: 'float' object is not callable
Our code returns an error. This is because we have assigned a floating point value to a variable called “float”. Later in our code, we try to use the float()
function to convert a value to a float. Because we have assigned “float” a numerical value, our code cannot call the float()
function.
To solve this problem, we need to rename our “float” variable:
staff_working = input("How many staff were working today? ") earned_in_tips = float(input("How much was earned in tips?")) staff_due = earned_in_tips / float(staff_working) rounded = round(staff_due, 2) print(rounded)
We have renamed the variable “float” to “earned_in_tips”. Let’s run our code:
How many staff were working today? 7 How much was earned in tips? 300 42.86
Our code runs successfully. Each member of the wait staff is due $42.86 in tips.
Scenario #2: Missing Mathematical Operator
The cause of the “TypeError: ‘float’ object is not callable” error can often be down to a missing mathematical operator.
The restaurant is offering a bonus program where the restaurant applies a 5% increase to all the tips earned in a day. This means that the wait staff will earn more money at the end of the day, depending on how much in tips they collect.
To account for this increase, we need to revise our formula for calculating the tips due to be given to the staff members:
staff_working = input("How many staff were working today? ") earned_in_tips = float(input("How much was earned in tips? ")) staff_due = 1.05 (earned_in_tips / float(staff_working)) rounded = round(staff_due, 2) print(rounded)
Our code calculates the amount each staff member is due by dividing how much is earned in tips by the number of staff working. We multiply this by 1.05 to calculate a 5% increase in the total tips due for each staff member. Let’s run our code:
How many staff were working today? 7 How much was earned in tips? 300 Traceback (most recent call last): File "main.py", line 4, in <module> staff_due = 1.05 (earned_in_tips / float(staff_working)) TypeError: 'float' object is not callable
We encounter an error. This is because we have forgotten a mathematical operator in our code. 1.05 is followed immediately by a set of parenthesis. Python treats this as a function call on the 1.05 value. Our “staff_due” formula should include a multiplication sign (*):
staff_due = 1.05 * (earned_in_tips / float(staff_working))
Our new code separates the 1.05 value and the result of our math equation in brackets with a multiplication sign. Let’s run our code:
How many staff were working today? 7 How much was earned in tips? 300 45.0
Each member of the wait staff is entitled to $45.00 from the tip pot. This includes the 5% bonus the restaurant is offering its staff.
Conclusion
The “TypeError: ‘float’ object is not callable” error is raised when you try to call a floating-point number as a function.
You can solve this problem by ensuring that you do not name any variables “float” before you use the float()
function. If that does not solve the problem, make sure that your code includes all the right mathematical operands.
Now you’re ready to solve this common Python error like a pro!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.
An other case is:
range = 0.5 – 0,23
…
..
for i in range(number): # error not related to variable number
will fail because “range” function has been overwriten by range float variable.
Xtian.