Python TypeError: ‘float’ object is not callable

The TypeError: ‘float’ object is not callable error occurs if you call floating-point value as a function or if an arithmetic operator is missed while performing the calculations or the reserved keywords are declared as variables and used as functions, 

In this tutorial, we will learn what float object is is not callable error means and how to resolve this TypeError in your program with examples.

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

There are two main scenarios where developers get this TypeError is:

  1. When you try to call the reserved keywords as a function
  2. Missing an Arithmetic operator while performing the calculation

Scenario 1: When you try to call the reserved keywords as a function

Using the reserved keywords as variables and calling them as functions are developers’ most common mistakes when they are new to Python. Let’s take a simple example to reproduce this issue.

item_price = [5.2, 3.3, 5.4, 2.7]
sum = 5.6
sum = sum(item_price)
print("The sum of all the items is:", str(sum))

Output

Traceback (most recent call last):
  File "c:\Personal\IJS\Code\main.py", line 3, in <module>
    sum = sum(item_price)
TypeError: 'float' object is not callable

If you look at the above code, we have declared the sum as a variable and stored a floating-point value. However, in Python, the sum() is a reserved keyword and a built-in method that adds the items of an iterable and returns the sum.

Since we have declared sum as a variable and used it as a function to add all the items in the list, Python will throw TypeError.

Solution

We can fix this error by renaming the sum variable to total_price, as shown below.

item_price = [5.2, 3.3, 5.4, 2.7]
total_price = 5.6
total_price = sum(item_price)
print("The sum of all the items is:", str(total_price))

Output

The sum of all the items is: 16.6

Scenario 2: Missing an Arithmetic operator while performing the calculation

While performing mathematical calculations, if you miss an arithmetic operator within your code, it leads to TypeError: ‘float’ object is not callable error.

Let us take a simple example to calculate the tax for the order. In order to get the tax value, we need to multiply total_value*(tax_percentage/100).


item_price = [5.2, 3.3, 5.4, 2.7]
tax_percentage = 5.2
total_value = sum(item_price)
tax_value = total_value(tax_percentage/100)
print(" The tax amount for the order is:", tax_value)

Output

Traceback (most recent call last):
  File "c:\Personal\IJS\Code\main.py", line 5, in <module>
    tax_value = total_value(tax_percentage/100)
TypeError: 'float' object is not callable

We have missed out on the multiplication operator while calculating the tax value in our code, leading to TypeError by the Python interpreter.

Solution

We can fix this issue by adding a multiplication (*) operator to our code, as shown below.


item_price = [5.2, 3.3, 5.4, 2.7]
tax_percentage = 5.2
total_value = sum(item_price)
tax_value = total_value*(tax_percentage/100)
print(" The tax amount for the order is:", tax_value)

Output

 The tax amount for the order is: 0.8632000000000002

Conclusion

The TypeError: ‘float’ object is not callable error raised when you try to call the reserved keywords as a function or miss an arithmetic operator while performing mathematical calculations.

Developers should keep the following points in mind to avoid the issue while coding.

  • Use descriptive and unique variable names. 
  • Never use any built-in function, modules, reserved keywords as Python variable names.
  • Ensure that arithmetic operators is not missed while performing calculations.
  • Do not override built-in functions like sum()round(), and use the same methods later in your code to perform operations.
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like