4

so i'm coding a tic tac toe game in python 3.2 and i have spent night and day trying to fix this and going over my code, script, or whatever you want to call it, so many times and still can't find it. i've googled it and all the answers are all to confusing or the person is scripting something way different from my tic tac toe game. and please beware, i'm just a beginner at python. when i try and run it an error comes up:

Traceback (most recent call last):
  File "/Users/user/Desktop/tic tac toe game.py", line 41, in <module>
    input = input("Select a spot:")
TypeError: 'int' object is not callable"

what does that mean? here's the code it says it's having a problem with:

while True:

    input = input("Select a spot:")
    input = int(input)

if you could help me, that would mean so much. it's been so annoying and i've been trying my hardest to fix it.

Bart
  • 19,692
  • 7
  • 68
  • 77
Ali Lewis
  • 47
  • 1
  • 1
  • 6

3 Answers3

12

input() is a Python function, and you are using it both as the function and an identifier.

Using input as a variable name will work the first time, but the 2nd time through the loop there won't be an input() function any longer as that name now is associated with an integer variable.

So instead of function input() you just have a variable named input, hence the error (also as mentioned by @poke and @DSM in the comments)

Using answer as your variable name would be a better idea:

while True:
    answer = input("Select a spot:")
    answer = int(answer)
Levon
  • 138,105
  • 33
  • 200
  • 191
  • 1
    You're correct about it not being a good idea, but I doubt that's the problem here. – martineau Jun 25 '12 at 16:53
  • 4
    @martineau It is because the assignment is in a loop. The first iteration will go well but in the second, `input` is no longer callable. – poke Jun 25 '12 at 16:55
  • @Ali Lewis: It's OK to use `input()`, just don't assign the result to a variable of the same name. – martineau Jun 25 '12 at 16:57
  • @AliLewis: `answer = int(answer)` would be OK. – martineau Jun 25 '12 at 17:01
  • here click this: http://i47.tinypic.com/fvy16s.jpg ~ that's what it looks like after the error pops up. – Ali Lewis Jun 25 '12 at 17:07
  • dude.. you're a lifesaver.. but now -_- i have another problem.. i'm so sorry.. but click here: http://i47.tinypic.com/10z4rwm.jpg it like says there's a break or something in the loop.. i know there is a way to fix it with a : or a ; or something.. but i haven't memorized it. – Ali Lewis Jun 25 '12 at 17:49
  • i clicked the check.. so would i just delete the break? – Ali Lewis Jun 25 '12 at 18:09
  • okay.. i just deleted the break.. but this goes back to my original question.. answer = int(answer) <- that's what i changed it to and now it says the answer isn't defined or whatever? it says, "NameError: name 'answer' is not defined. – Ali Lewis Jun 25 '12 at 18:15
5

You're using the name input for the result, which replaces the input function you're trying to call. Use a different name.

Edit: When you loop through, by the time the second iteration rolls around, you have rebound the variable input to your user input, so it's not longer a builtin function, it's the integer you just converted the user input to. The name input can't refer to two different things at the same time.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
3

The first time you call

input("Select a spot:")

it works properly - returns a string, you convert it to int and store it to input.

The second time you call input, it takes the integer you just stored and tries to run it as a function. This fails, with the error message you have seen.

Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99