'"TypeError: 'str' object is not callable" when code is in while loop

This is a very simple program that randomly selects 2 values from a numbers list, adds them and saves them into a variable and then asks the user to type the correct answer. If the answer is right it prints that its right and if its not it prints that its wrong. The program runs without any problems if its not in a while loop but when its in a while loop and the program asks me for input and I input a value I get this error code:

    line 16, in <module>
    input = input(str(num1) + " + " + str(num2) + " = ")
TypeError: 'str' object is not callable

Program in while loop where the error occurs:

import random
import os

while True:

    os.system('cls||clear')

    numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]

    num1 = random.choice(numbers)

    num2 = random.choice(numbers)

    answer = num1 + num2

    input = input(str(num1) + " + " + str(num2) + " = ")

    if int(input) == answer:
        os.system('cls||clear')
        print("right")
    else:
        os.system('cls||clear')
        print("wrong: " + str(num1) + " + " + str(num2) + " " + "=" + " " + str(answer))

Program without while loop that runs without problems:

import random
import os

os.system('cls||clear')

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]

num1 = random.choice(numbers)

num2 = random.choice(numbers)

answer = num1 + num2

input = input(str(num1) + " + " + str(num2) + " = ")

if int(input) == answer:
    os.system('cls||clear')
    print("right")
else:
    os.system('cls||clear')
    print("wrong: " + str(num1) + " + " + str(num2) + " " + "=" + " " + str(answer))



Solution 1:[1]

input = input(...) runs well the first time, however, this sets the value of the name input to the value that was input by the user, which means input now is a string and not the input function, so a second call (input(...)) results in an error saying that you can not treat a string value as a function (call it in this case). It is always advisable to not use built-in names as your own variable names, so you should change the input variable to something else, like guess = input(...) or attempt = input(...) or something useful based on your context.

Solution 2:[2]

Your input variable should not be called input as it is a protected name. Rename it to something like userInput.

Solution 3:[3]

You redefine input to be a string. The second time it's called you try to call a str object, which produces the above error. Try renaming your input variable to something else, maybe answer?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MrGeek
Solution 2 oflint_
Solution 3 vidstige