In Python, a “Typeerror” occurs when you use different data types in an operation.
For example, if you attempt to divide an integer (number) by a string, it leads to a typeerror
because an integer data type is not the same as a string.
One of those type errors is the “int object is not callable” error.
The “int object is not callable” error occurs when you declare a variable and name it with a built-in function name such as int()
, sum()
, max()
, and others.
The error also occurs when you don’t specify an arithmetic operator while performing a mathematical operation.
In this article, I will show you how the error occurs and what you can do to fix it.
How to Fix Typeerror: int object is not callable
in Built-in Function Names
If you use a built-in function name as a variable and call it as a function, you’ll get the “int object is not callable” error.
For instance, the code below attempts to calculate the total ages of some kids with the built-in sum()
function of Python. The code resulted in an error because the same sum
has already been used as a variable name:
kid_ages = [2, 7, 5, 6, 3]
sum = 0
sum = sum(kid_ages)
print(sum)
Another example below shows how I tried to get the oldest within those kids with the max()
function, but I had declared a max
variable already:
kid_ages = [2, 7, 5, 6, 3]
max = 0
max = max(kid_ages)
print(max)
Both code examples led to this error in the terminal:
To fix the issue, you need to change the name of the variable you named as a built-in function so the code can run successfully:
kid_ages = [2, 7, 5, 6, 3]
sum_of_ages = 0
sum = sum(kid_ages)
print(sum)
# Output: 23
kid_ages = [2, 7, 5, 6, 3]
max_of_ages = 0
max = max(kid_ages)
print(max)
# Output: 7
If you get rid of the custom variables, your code will still run as expected:
kid_ages = [2, 7, 5, 6, 3]
sum = sum(kid_ages)
print(sum)
# Output: 23
kid_ages = [2, 7, 5, 6, 3]
max = max(kid_ages)
print(max)
# Output: 7
How to Fix Typeerror: int object is not callable
in Mathematical Calculations
In Mathematics, if you do something like 4(2+3), you’ll get the right answer which is 20. But in Python, this would lead to the Typeerror: int object is not callable
error.
To fix this error, you need to let Python know you want to multiply the number outside the parentheses with the sum of the numbers inside the parentheses.
To do this, you do this by specifying a multiplication sign (*) before the opening parenthesis:
print(4*(2+3))
#Output: 20
Python allows you to specify any arithmetic sign before the opening parenthesis.
So, you can perform other calculations there too:
print(4+(2+3))
# Output: 9
print(4-(2+3))
# Output: -1
print(4/(2+3))
# Output: 0.8
Final Thoughts
The Typeerror: int object is not callable
is a beginner error in Python you can avoid in a straightforward way.
As shown in this article, you can avoid the error by not using a built-in function name as a variable identifier and specifying arithmetic signs where necessary.
Thank you for reading.
Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started
Table of Contents
Hide
- What is TypeError: the ‘int’ object is not callable?
- Scenario 1: When you try to call the reserved keywords as a function
- Solution
- Scenario 2: Missing an Arithmetic operator while performing the calculation
- Solution
- Conclusion
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.
There are two main scenarios where developers try to call an integer.
- When you try to call the reserved keywords as a function
- 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:PersonalIJSCodemain.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:PersonalIJSCodemain.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.
Srinivas Ramakrishna is a Solution Architect and has 14+ Years of Experience in the Software Industry. He has published many articles on Medium, Hackernoon, dev.to and solved many problems in StackOverflow. He has core expertise in various technologies such as Microsoft .NET Core, Python, Node.JS, JavaScript, Cloud (Azure), RDBMS (MSSQL), React, Powershell, etc.
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.
By checking this box, you confirm that you have read and are agreeing to our terms of use regarding the storage of the data submitted through this form.
Error TypeError: ‘int’ object is not callable
This is a common coding error that occurs when you declare a variable with the same name as inbuilt int() function used in the code. Python compiler gets confused between variable ‘int’ and function int() because of their similar names and therefore throws typeerror: ‘int’ object is not callable error.
To overcome this problem, you must use unique names for custom functions and variables.
Example
##Error Code
#Declaring and Initializing a variable
int = 5;
#Taking input from user {Start}
productPrice = int(input("Enter the product price : "))
productQuantity = int(input("Enter the number of products : "))
#Taking input from user {Ends}
# variable to hold the value of effect on the balance sheet
# after purchasing this product.
costOnBalanceSheet = productPrice * productQuantity
#printing the output
print(costOnBalanceSheet)
Output
Enter the product price : 2300
Traceback (most recent call last):
File "C:UsersWebartsolAppDataLocalProgramsPythonPython37-32intObjectnotCallable.py", line 3, in <module>
productPrice = int(input("Enter the product price : "))
TypeError: 'int' object is not callable
In the example above we have declared a variable named `int` and later in the program, we have also used the Python inbuilt function int() to convert the user input into int values.
Python compiler takes “int” as a variable, not as a function due to which error “TypeError: ‘int’ object is not callable” occurs.
How to resolve typeerror: ‘int’ object is not callable
To resolve this error, you need to change the name of the variable whose name is similar to the in-built function int() used in the code.
#Code without error
#Declaring and Initializing a variable
productType = 5;
#Taking input from user {Start}
productPrice = int(input("Enter the product price : "))
productQuantity = int(input("Enter the number of products : "))
#Taking input from user {Ends}
# variable to hold the value of effect on the balance sheet
# after purchasing this product
costOnBalanceSheet = productPrice * productQuantity
#printing the output
print(costOnBalanceSheet)
OUTPUT:
Enter the product price : 3500
Enter the number of products : 23
80500
In the above example, we have just changed the name of variable “int” to “productType”.
How to avoid this error?
To avoid this error always keep the following points in your mind while coding:
- Use unique and descriptive variable names
- Do not use variable name same as python in-built function name, module name & constants
- Make the function names descriptive and use docstring to describe the function
In this article, we will be discussing the TypeError: “int” Object is not callable exception. We will also be through solutions to this problem with example programs.
Why Is This Error Raised?
- “int” is used as a variable name.
- Arithmetic Operator not provided when calculating integers.
- Placing parentheses after a number
Using int As A Variable, Name
Variable declaration using in-built names or functions is a common mistake in rookie developers. An in-built name is a term with its value pre-defined by the language itself. That term can either be a method or an object of a class.
int
is an in-built Python keyword. As we discussed, it is not advisable to use pre-defined names as variable names. Although using a predefined name will not throw any exception, the function under the name will no longer be re-usable.
Let’s refer to the following example:
myNums = [56,13,21,54] sum = 0 sum = sum(myNums) print("sum of myNums list: ", sum)
Output and Explanation
Srinivas Ramakrishna is a Solution Architect and has 14+ Years of Experience in the Software Industry. He has published many articles on Medium, Hackernoon, dev.to and solved many problems in StackOverflow. He has core expertise in various technologies such as Microsoft .NET Core, Python, Node.JS, JavaScript, Cloud (Azure), RDBMS (MSSQL), React, Powershell, etc.
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.
By checking this box, you confirm that you have read and are agreeing to our terms of use regarding the storage of the data submitted through this form.