Python TypeError: ‘int’ object is not callable

The TypeError: the ‘int’ object is not a callable error occurs 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 int object is is not callable error means and how to resolve this TypeError in your program with examples.

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

There are two main scenarios where developers try to call an integer.

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 = [10, 33, 55, 77]
sum = 0
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 4, in <module>
sum = sum(item_price)
TypeError: 'int' object is not callable

If you look at the above code, we have declared the sum as a variable. 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 = [10, 33, 55, 77]
total_price = 0
total_price = sum(item_price)
print("The sum of all the items is:", str(total_price))

Output

The sum of all the items is: 175

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: the ‘int’ object is not a 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 = [10, 23, 66, 45]
tax_percentage = 5
total_value = sum(item_price)
tax_value = total_value(5/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 8, in <module>
tax_value = total_value(5/100)
TypeError: 'int' 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 = [10, 23, 66, 45]
tax_percentage = 5
total_value = sum(item_price)
tax_value = total_value*(5/100)
print(" The tax amount for the order is:", tax_value)

Output

The tax amount for the order is: 7.2

Conclusion

The TypeError: the ‘int’ object is not a 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.

Get notified of the best deals on our WordPress themes.

You May Also Like JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Table of Contents Hide JSONDecodeError: Expecting value: line 1 column 1 (char 0)Example JSONDecodeError: Expecting value: line 1 column 1 (char 0)Solution Other Possible SolutionsEnsure HTTP 200 status code and valid… Python ValueError: could not convert string to float

Table of Contents Hide ValueError: could not convert string to floatException could not convert string to float Fix ValueError: could not convert string to floatSolution 1: Ensure the string has… ImportError: attempted relative import with no known parent package

Table of Contents Hide How does module import work in Python?Absolute vs. Relative importsHow to fix ImportError: attempted relative import with no known parent package?Option 1 – Use absolute importsOption… Python FileNotFoundError: [Errno 2] No such file or directory Solution

Table of Contents Hide Python FileNotFoundError: [Errno 2] No such file or directoryExample FileNotFoundErrorMisspelled file nameInvalid file path or directory pathUsing a relative pathSolution to FileNotFoundError: [Errno 2] No such… TypeError: list indices must be integers or slices, not tuple

If you are accessing the list elements in Python, you need to access it using its index position. If you specify a tuple or a list as an index, Python… List of Free Python Books

Table of Contents Hide Python SuccinctlyPython Data Science HandbookThink Python 2nd EditionAutomate the Boring Stuff with PythonMaking Games with Python & PygameData Structures and Algorithms in Python Google Python Style GuideA…