. Advertisement .
..3..
. Advertisement .
..4..
There are different types of errors in Python like keyError, AttributeError, SyntaxError, NameError, etc. There is one more error that is most common is TypeError. The main cause of this error is when trying some operation on a datatype and that particular operation is not applicable on that specific datatype. Here, we are going to highlight a TypeError in python ‘TypeError: ‘int’ object is not callable’.
How to Fix TypeError: ‘int’ object is not callable
The root causes of TypeError: ‘int’ object is not callable are
- Same name for int function and variable
- Missing Arithmetic operator
- Parenthesis after a number
Checkout each root cause with the example and solution
Using the same name for int function and variable
This one is the most common mistake that most Python learners make when designing a program. For example, you want to determine the total sum of a bill list, and you use a built-in function of Python ‘sum()
’ to calculate the sum list and then use the same name as the variable name.
This is exactly when you encounter TypeError: ‘int’ object is not callable
Code Example
bill = [28, 22, 29, 88, 89, 15, 18]
# total integer sum
sum = 0
# using the inbuilt Python sum method
sum = sum(bill)
print("The total bill is: ",sum)
Error message
Traceback (most recent call last):
File "main.py", line 7, in <module>
sum = sum(bill)
TypeError: 'int' object is not callable
Here, a new int variable is defined by the name sum=0, the Python program treats the sum as an int. At the same time, we also use sum as a function to calculate the sum of the bill, which makes the Python confused with the names. And, as a result, Python treats sum as an int object instead of a built-in function, so it returns TypeError: 'int' object is not callable
.
Solution
Just change the int name
bill = [28, 22, 29, 88, 89, 15, 18]
# total integer
result = 0
# using the inbuilt Python sum method
result = sum(bill)
print("The total bill is: ",result)
Output
The total bill is: 289
Missing Arithmetic Operator
The absence of an arithmetic operation in an equation causes the error
Code Example
prices = [100,150,200,250,330]
discount = 20
sumPrice = sum(prices)
discountAmount = sumPrice(discount/100)
print("Total discount amount: ", discountAmount)
Error message
File "C:\Users\Admin\.spyder-py3\temp.py", line 4, in <module>
discountAmount = sumPrice(discount/100)
TypeError: 'int' object is not callable
Missing an operator in the discountAmount causes the error
Solution
prices = [100,150,200,250,330]
discount = 20
sumPrice = sum(prices)
discountAmount = sumPrice*(discount/100)
print("Total discount amount: ", discountAmount)
Output
Total discount amount: 206.0
Parenthesis Placement After an Int
Using parenthesis exactly after an integer leads to an error. Denote the right operator instead of just bracketing the raw integer.
Code Example
99(999)
Error message
File "C:\Users\Admin\.spyder-py3\temp.py", line 2, in <module>
99(999)
TypeError: 'int' object is not callable
Solution
99 * 999
Conclusion
We shed light on the exception TypeError: ‘int’ object is not callable and how to tackle it. Basic flaws in the code can lead to this error. It’s time to complete your project where you have been witnessing this TyprError.
Leave a comment