In this article, we will explain to you on how to fix the typeerror: int object is not callable error.
Python developers may often encounter an error and one of these errors is int object is not callable.
Why this python typeerror: ‘int’ object is not callable occur?
This python typeerror: ‘int’ object is not callable usually occurs once you are declaring a variable and naming it with a built-in function name like int(), sum(), max(), and others.
In addition, the error message occurs when you try to call a function that is not callable.
Here is an example on how the error occurs:
x = 5
result = x(10)
In this example, we have assigned the value 5 to the variable x.
In the next line, we are trying to call x as a function and passing the argument 10 to it.
However, since x is not a function, Python raises the “TypeError ‘int’ object is not callable” error message.
Traceback (most recent call last):
File “C:\Users\Dell\PycharmProjects\pythonProject\main.py”, line 2, in
result = x(10)
TypeError: ‘int’ object is not callable
Causes the Error Message?
The “TypeError ‘int’ object is not callable” error message is caused by two main reasons:
- Trying to call a variable as a function.
- Using parentheses after a variable that is not a function.
How to solve the Typeerror: int object is not callable in Built-in Function Names
When we are using a built-in function name as a variable and we call it as a function, we will get the “int object is not callable” error.
For example:
kid_ages = [1, 3, 5, 7, 9]
sum = 0
sum = sum(kid_ages)
print(sum)
The list of numbers is defined on the first line of the program as “kid_ages”, with the values [1, 3, 5, 7, 9].
Next, a variable named “total_sum” is initialized to zero.
Then, the built-in Python function “sum()” is called with the argument “kid_ages” to calculate the sum of all the values in the list.
The result is then assigned to the variable “total_sum”.
Finally, the result stored in “total_sum” is printed to the console using the “print()” function.
The output will raise an error:
Traceback (most recent call last):
File “C:\Users\Dell\PycharmProjects\pythonProject\main.py”, line 4, in
sum = sum(kid_ages)
TypeError: ‘int’ object is not callable
To solve this error, you need to modify the name of the variable you named as a built-in function.
For that, the code will run successfully:
kid_ages = [1, 3, 5, 7, 9]
total_sum = 0
total_sum = sum(kid_ages)
print(total_sum)
In this example code, the variable name “sum” has been changed to “total_sum” to avoid conflicts with the built-in function named “sum()”.
Solution to solve Typeerror: int object is not callable in Mathematical Calculations
In Math, when you do something like 10(5+8), you will get the right answer which is 130. Yet, if you do this in Python, you will get the Typeerror int object is not callable error.
C:\Users\Dell\PycharmProjects\pythonProject\main.py:1: SyntaxWarning: ‘int’ object is not callable; perhaps you missed a comma?
10(5+8)
Traceback (most recent call last):
File “C:\Users\Dell\PycharmProjects\pythonProject\main.py”, line 1, in
10(5+8)
TypeError: ‘int’ object is not callable
To solve this error, we should let Python know that we want to multiply the number outside the parentheses with the sum of the numbers inside the parentheses.
We can do this, through defining a multiplication sign (*) before the opening parenthesis:
For example:
print(10*(5+8))
The code “print(10*(5+8))” is a simple Python expression that prints the result of a mathematical calculation to the console.
The expression first evaluates the value inside the parentheses, which is the sum of two numbers: 5 and 8. This results in a value of 13.
Then, the expression multiplies this value by 10, resulting in a final value of 130.
The “print()” function is used to output this value to the console, so the output of the code will be:
130
In Python, you are able to use any arithmetic operator before an opening parenthesis to perform calculations within it.
Therefore, we can also perform multiple operations:
For addition
print(10+(5+8))
Output:
23
For subtraction:
print(10-(5+8))
Output:
-3
For division:
print(10/(5+8))
Output:
0.7692307692307693
Additional Resources
- Typeerror object of type decimal is not json serializable [SOLVED]
- typeerror: this.getoptions is not a function
- Typeerror: ‘float’ object cannot be interpreted as an integer
- Typeerror argument of type int is not iterable
- Typeerror can only concatenate str not nonetype to str
Conclusion
In conclusion, we’ve discussed on how to solve the error and also we provide some solutions to solve it.
Also, we’ve explained why this error occurs in provide some useful examples to follow.
FAQs
The error occurs when you did not define an arithmetic operator while performing a mathematical operation.
Yes, you can prevent this error message from occurring by ensuring that you are calling a function and not a variable, and by ensuring that you are using parentheses only after a function.