我正在通过一个
tutorial on GeekforGeeks website
注意到他们正在使用
board[x,y]
,这是我从未见过的。我不认为这行得通,但当我运行程序时,一切都按预期进行。
我试着用上面概述的方法运行一个更小的代码示例,而不是我更熟悉的方法(
board[x][y]
)但是当我运行代码时
TypeError: list indices must be integers or slices, not tuple
我的代码:
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)
他们的代码:
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)
有人能解释一下为什么吗
董事会
有效,到底发生了什么?我以前从来没有见过这个,除了创建列表,也没有从概念上理解它。