我在閱讀'用Python發明自己的遊戲',並且正在嘗試創建一本書中的遊戲。雖然我的代碼在書中的代碼一致,我仍然得到一個錯誤:我得到的錯誤列表索引超出範圍
File "/Users/Rocky/reverso.py", line 251, in <module>
resetBoard(mainBoard)
File "/Users/Rocky/reverso.py", line 27, in resetBoard
board[x][y] = ' '
IndexError: list index out of range
代碼:
def resetBoard(board):
for x in range(8):
for y in range(8):
board[x][y] = ' ' #This is line 27
board[3][3] = 'X'
board[3][4] = 'O'
board[4][3] = 'O'
board[4][4] = 'X'
def getNewBoard():
board = []
for i in range(8):
board.append([' '] * 8)
return board
while True: # line 248
mainBoard = getNewBoard()
resetBoard(mainBoard) # This is line 251
playerTile, computerTile = enterPlayerTile()
showHints = False
turn = whoGoesFirst()
print('The ' + turn + ' will go first.')
請添加'getNewBoard'的代碼。 –
我應該在哪裏添加它 –
在問題中。點擊[編輯]。 –