Ali Lewis
Ali Lewis

Reputation: 47

TypeError: list indices must be integers in Tic Tac Toe game - Python 3.2?

so i've had multiple problems with a tic tac toe game i'm making, but i've found solutions to most of the problems. but the problem now is that i get an error reading

Traceback (most recent call last):
  File "/Users/user/Desktop/tic tac toe game.py", line 43, in <module>
    if board[input] != 'x' and board[input] !='o':
TypeError: list indices must be integers, not builtin_function_or_method" 

when i try and run it. and here's the code it's having issues with:

if board[input] != 'x' and board[input] !='o':
        board[input] = 'x'

i'm not sure what to do and i like just started python on thursday and am not very smart with it.. but thanks for all the help (: oh and i have python 3.2.

Upvotes: 0

Views: 1666

Answers (1)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250911

looks like input is not an integer here, you've used input() here, which is a built-in function. Don't ever use input as a variable name too.

see same error:

>>> a=[1,2,3,4]
>>> a[input]

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    a[input]
TypeError: list indices must be integers, not builtin_function_or_method

to work around it, use:

>>> Board=['a','x','o','b','c','d']
>>> n=input()#input returns a string,not integer
2
>>> if Board[int(n)]!='x' and Board[int(n)]!='o': #list expect  only integer indexes,convert n to integer first using
    Board[int(n)]='x'


>>> Board
['a', 'x', 'o', 'b', 'c', 'd']

Upvotes: 2

Related Questions