[‘Fixed’] TypeError: ‘int’ object is not callable

Summary: TypeError: ‘xyz’ object is not callable is usually raised when you try to call a method with the same name as a variable in the code. To avoid this error, rename the variable and avoid using variables with names like built-in methods in Python.

Have you come across TypeErrors as:

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️
  • TypeError: ‘int’ object is not callable
  • TypeError: ‘float’ object is not callable
  • TypeError: ‘str’ object is not callable

If the above errors keep bugging you then you have come to the right place to get rid of your problem. In this tutorial, we will learn about the TypeError: ‘xyz’ object is not callable. Since any concept is best understood with the help of examples, we will walk through numerous scenarios and resolve the error in the process. So, without further delay,let’s dive into the discussion.

 Type 1: TypeError: ‘int’ object is not callable

Scenario 1: Overriding a built-in function that you use later in your code as a variable

👉 If you declare a variable with the name int and you also have a variable that accepts the user input with the help of the int() method, then the Python is unable to distinguish between the inbuilt int() method and the declared variable int in the program.

Example 1: Suppose you are writing a program to swap the values of two variables using a third variable.

Output:

Traceback (most recent call last):
File “D:/PycharmProjects/pythonProject1/int object not callable.py”, line 2, in
a = int(input(‘Enter the first no.: ‘))
TypeError: ‘int’ object is not callable

Solution: The error occurred because the third variable declared in the program has the name int. To resolve the issue rename for the variable int variable to something else, for exampletemp.

Output:

Enter the first no.: 10
Enter the second no: 20
Before Swapping: a=10 and b=20
After Swapping: a=20 and b=10

Example 2:

Output:

Traceback (most recent call last):
File “D:/PycharmProjects/pythonProject1/int object not callable.py”, line 4, in
print(round((a/b)*0.9*round))
TypeError: ‘int’ object is not callable

Solution: round() is an in-built function in Python. So, when you declare a variable with the same name as round it leads to the occurrence of TypeError. To avoid this error rename the round variable to avoid conflict between the variable and function name.

Output:

7

Exercise: Time to test your knowledge! Find the error in the program given below and rectify it to generate the output.

Solution:

Scenario 2: Missing an Arithmetic Operator

If you accidentally miss a mathematical operator in a certain calculation within your code then it leads to TypeError: 'int' object is not callable.

Example: The following code computes the Perimeter of a rectangle based of the formula P= 2(l + b).

Output:

Enter the length: 10
Enter the breadth: 5
Traceback (most recent call last):
File “main.py”, line 3, in
p = 2(l+b)
TypeError: ‘int’ object is not callable

Solution: In the above code we missed to enter the arithmetic operator * in line 3 which lead to the error. Simply include this operator to avoid the error.

Output:

Enter the length: 10
Enter the breadth: 5
Perimeter of rectangle = 30

Type 2: TypeError: ‘float’ object is not callable

The reasons behind the occurrence of TypeError: 'float' object is not callable are similar, i.e.,

  • Case 1: When you try to call a method when a property with the same name is available in code.
  • Case 2: When you have forgotten to include a mathematical operator in the calculation.

Exercise: Find the error in the code given below and generate the output.

hint: Line 3 looks fishy!

Solution:

Type 3: TypeError: ‘str’ object is not callable

Python throws TypeError: 'str' object is not callable in two scenarios:

Scenario 1: Declaring a Variable Named str along with the str() Method

Consider the example given below.

Output:

Enter your name: Shubham
Enter your age: 25
Traceback (most recent call last):
File “D:/PycharmProjects/pythonProject1/int object not callable.py”, line 3, in
print(str(str+” is”+age+” years old.”))
TypeError: ‘str’ object is not callable

Solution: Rename the str variable.

Output:

Enter your name: Shubham
Enter your age: 25
Shubham is 25 years old.

Scenario 2: Incorrect Usage of the % Operator while Formatting a String

If you forget to include the % operator to separate your string and the values you want to add to our string then it leads to the occurrence of TypeError: 'str' object is not callable.

Example:

Output:

Enter your name: Shubham
Enter your age: 25
Traceback (most recent call last):
File “main.py”, line 3, in
print(“%s is %s years old.”(name,age))
TypeError: ‘str’ object is not callable

Solution: Ensure that you do not miss the % operator in your print statement.

Output:

Enter your name: Shubham
Enter your age: 25
Shubham is 25 years old.

Conclusion

You might want to look at the article given to visualize errors that are similar to those mentioned in this article.

Read here: [Solved] TypeError: ‘Module’ Object Is Not Callable in Python?

Please subscribe and stay tuned for more interesting articles. Happy learning! 📚

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.