Symacs
Symacs

Reputation: 33

Python help: 'str' object is not callable

I am learning Python, getting this error in my code. Searched online, but didn't find an answer I understand. Can someone help?

TypeError: 'str' object is not callable

Here is my code:

while True:
    print("Enter 'add' to add two numbers")
    print("Enter 'quit' to quit the program")
    user_input = input(": ")

    if user_input == "quit":
        break
    elif user_input == "add":
        num1 = float(input("Enter 1st number: "))
        num2 = float(input("Enter 2nd number: "))
        result = str(num1 + num2)
        print("Answer is: " + result)

Upvotes: 2

Views: 4658

Answers (1)

Jean-François Fabre
Jean-François Fabre

Reputation: 140188

it's what happens when you're using built-in functions to define your variables:

>>> str = "string"
>>> str(12)
Traceback (most recent call last):
  File "<string>", line 301, in runcode
  File "<interactive input>", line 1, in <module>
TypeError: 'str' object is not callable

this kind of str assignment must have been performed before this code, which is okay. Quickfix:

del str

Proper fix: find where this variable is defined and rename / delete it.

(note that input or float could be the names that have been redefined)

Upvotes: 4

Related Questions