[‘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:

  • 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! 📚


import_contacts

You may also like:

Related Posts

  • Convert dict to String in Python
    17 December

    Convert dict to string in python

    Table of ContentsHow to convert dict to string in python?Using str() method.Using the pickle module.Using the for loop.Using the json.dumps() method. In this post, we will see how to convert dict to String in Python. Python contains several data structures, and more often than not, there is a need for the conversion of one type […]

  • Python range float
    17 December

    Generate float range in Python [9 ways]

    Table of ContentsWhat is a floating-point number in Python?Why not use the simple and popular range() function?Python range float : How to generate floating-point numbers in Python?Using the numpy.linspace() method.Using the numpy.arange() method.Using list comprehension.Using generator comprehension.Using yield generator.Using a user-defined function.Using the itertools module and its functions.Using the map() function along with a lambda […]

  • Python callback function
    13 December

    Callback function in Python

    Table of ContentsWhat is Callback Function in Python?Applications of Callback Functions in PythonConclusion In python, functions are first-class objects. In other words, we can pass a function to a different function as an input argument. We can return a function from another function, and we can assign a function to a variable. In this article, […]

  • ValueError: too many values to unpack
    10 December

    [Fixed] ValueError: too many values to unpack (expected 2)

    Table of ContentsWhat is unpacking in python?What is ValueError: too many values to unpack (expected 2)?How to resolve ValueError: too many values to unpack (expected 2)?Conclusion While programming in python, we can assign values to two or more variables in a single statement. While doing so, you might get the ValueError exception with the message […]

  • Python List of dictionaries
    09 December

    List of Dictionaries in Python

    Table of ContentsHow to Create a List of Dictionaries in Python?How to Access Elements of a List of Dictionaries?Search in a List of Dictionaries in PythonUpdate Values in a List of Dictionaries in PythonHow to Sort List of Dictionaries in Python?Conclusion We use both lists and dictionaries in our python programs. Lists are used to […]

  • Python if not
    08 December

    Python If with NOT Operator

    Table of ContentsNOT Operator and IF Statement in PythonApplications of the if statement with not operator in pythonConclusion In python, we use conditional statements to implement logic in our program. For this, we use boolean expressions with if statements. In this article, we will see how we can use the if statement with the not […]

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.