I'm working through a tutorial on GeekforGeeks website and noticed that they are checking a point in an array using board[x,y]
, which I've never seen before. I don't think this would work, but when I run the program, everything goes as expected.
I tried running a smaller code example using their method outlined above vs the method I'm more familiar with (board[x][y]
), but when I run my code, I get TypeError: list indices must be integers or slices, not tuple
My code:
board = [[1,1,1], [1,2,2], [1,2,2]]
win = 'True'
if board[1][1] == 2:
win = 'True by normal standards'
print(win)
if board[1, 1] == 2:
win = 'True by weird standards'
print(win)
print(win)
Their code:
def row_win(board, player):
for x in range(len(board)):
win = True
for y in range(len(board)):
if board[x, y] != player:
win = False
continue
if win == True:
return(win)
return(win)
Can someone explain to me why board[x,y]
works, and what exactly is happening? I've never seen this before except to create lists, and am not grasping it conceptually.
python -i
), thentype(board)
will show you what typeboard
is (numpy.ndarray
orpandas.DataFrame
). Or look at the lines that create and initializeboard
.