'str' object isn't callable

Help! My calculator project is stuck at ‘str’ object isn’t callable. Are there anything wrong?

def add(x,y):
   return x + y
def subtract(x,y) :
    return x - y
def multiply(x,y) :
    return x * y
def devide(x,y) :
    return x / y
print("Enter your choice(from 1 to 4)")
print("1 : add")
print("2 : subtract")
print("3 : multiply")
print("4 : divide")
input = input()
if input == "1":
    print("What's your calculation?")
    num1 = float(input("First number : "))
    num2 = float(input("Second number : "))
    print(num1,"+",num2,"=", add(num1,num2))
elif input == "2":
    print("What's your calculation?")
    num1 = float(input("First number : "))
    num2 = float(input("Second number : "))
    print(num1,"-",num2,"=", subtract(num1,num2))
elif input == "3":
    print("What's your calculation?")
    num1 = float(input("First number : "))
    num2 = float(input("Second number : "))
    print(num1,"*",num2,"=", multiply(num1,num2))
elif input == "4":
    print("What's your calculation?")
    num1 = float(input("First number : "))
    num2 = float(input("Second number : "))
    print(num1,"/",num2,"=", devide(num1,num2))
else :
    print("Invalid input")

Hi, welcome to the forum! I’ve edited your post, so that the formatting of your code is preserved. By default, the forum doesn’t preserve whitespace and indentation - the latter being of critical importance in Python, as that’s how it demarcates code blocks.

In future, please format your code so it’s easier for us to help you. The Codecademy forum provides a quick and easy way to post properly-formatted code. All you need to do is look for the </> icon in the menu bar which appears at the top of the text box while you type.

capture

Press Enter to go to a blank line, click that icon, and you will see this:

capture_r

Just copy and paste your code directly from the editor into that highlighted portion, and all your lovely formatting, indentation etc, will be kept.

On to your question. Your problem is here:

Whilst you can use the word “input” as a variable name in Python, you probably shouldn’t. This is because doing so will override the built-in function of the same name, input(). Watch what happens:

# I need a copy of the standard input function to use later!
std_input = input

# let's redefine what we want input to do!
def new_input(prompt):
    str_in = std_input(prompt) # std_input == input, we've just renamed it!
    str_out = ""
    for char in str_in:
        str_out = char + str_out
    return str_out

# Let's get some standard input
std_in = input("What's your name: ") # Adam
print("Your name is ", std_in) # Your name is  Adam

# Now, let's override the input function!
input = new_input

# ...and try again
new_in = input("What's your name: ") # Adam
print("Your name is ", new_in) # Your name is  madA

Your line input = input() is overriding the definition of the input function with the return value from the function call. This means that input goes from meaning “get input from the user” to, say, a static value of "3" if I wanted to do multiplication.

The string "3" isn’t a function we can invoke, in the same way we can input(), hence your error from the Traceback.

To fix it, all you need to do is rename your input variable to something else. :slight_smile:

1 Like

Thank you, you solved my problem!

1 Like