有人可以告诉我错误在哪里吗?我已经绕了一个星期来解决这个问题。我收到错误 “ TypeError:列表索引必须是整数或切片,而不是列表” 表示要在播放器选择索引的位置放置“ X”的playerXturn()函数中的代码行。
def main():
gameBoard = [[" ", " ", " "],
[" ", " ", " "],
[" ", " ", " "]]
printBoard(gameBoard)
playerXturn(gameBoard)
while boardFull(gameBoard) is False:
if diagonalWin(gameBoard) is False and rowWin(gameBoard) is False:
playerOturn(gameBoard)
if diagonalWin(gameBoard) is False and rowWin(gameBoard) is False:
playerXturn(gameBoard)
else:
print("Player X wins!")
else:
print("Player O wins!")
printBoard(gameBoard)
def printBoard(gameBoard):
print("---------")
print(gameBoard[0][0], "|", gameBoard[0][1], "|", gameBoard[0][2])
print("---------")
print(gameBoard[1][0], "|", gameBoard[1][1], "|", gameBoard[1][2])
print("---------")
print(gameBoard[2][0], "|", gameBoard[2][1], "|", gameBoard[2][2])
print("---------")
def playerXturn(gameBoard):
playerXrowChoice = input("Enter a row (0, 1, or 2) for player X: ")
playerXrow = [eval(x) for x in playerXrowChoice]
playerXcolumnChoice = input("Enter a column (0, 1, or 2) for player X: ")
playerXcolumn = [eval(x) for x in playerXcolumnChoice]
if gameBoard[playerXrow][playerXcolumn] != "X" and gameBoard[playerXrow][playerXcolumn] != "O":
gameBoard[playerXrow, playerXcolumn] = "X"
else:
print("This spot is already taken.")
return gameBoard
def playerOturn(gameBoard):
playerOrowChoice = input("Enter a row (0, 1, or 2) for player X: ")
playerOrow = [eval(x) for x in playerOrowChoice]
playerOcolumnChoice = input("Enter a column (0, 1, or 2) for player X: ")
playerOcolumn = [eval(x) for x in playerOcolumnChoice]
if gameBoard[playerXrow][playerXcolumn] != "X" and gameBoard[playerXrow][playerXcolumn] != "O":
gameBoard[playerOrow, playerOcolumn] = "O"
else:
print("This spot is already taken.")
return gameBoard
#check for empty spaces on the board
def boardFull(gameBoard):
for i in range(len(gameBoard)):
for j in range(len(gameBoard[i])):
if j != " ":
return True
else:
return False
#check for diagonal match
def diagonalWin(b):
while b[1][1] != " ":
if b[1][1] == b[0][0] == b[2][2] or b[1][1] == b[0][2] == b[2][0]:
return True
else:
return False
#check for 3 in a row
def rowWin(b):
for row in range(0, 3):
if b[row][0] == b[row][1] == b[row][2] == 'X':
return True
else:
return False
main()
答案 0 :(得分:1)
错误在这一行:
gameBoard[playerXrow, playerXcolumn] = "X"
我想你的意思是
gameBoard[playerXrow][playerXcolumn] = "X"
答案 1 :(得分:0)
我是python的新手,所以我不知道eval()的作用,但是我弄清楚了错误在哪里。
def playerXturn(gameBoard):
playerXrowChoice= input("Enter a row (0, 1, or 2) for player X: ")
playerXrow = [eval(x) for x in playerXrowChoice]
playerXcolumnChoice= input("Enter a column (0, 1, or 2) for player X: ")
playerXcolumn = [eval(x) for x in playerXcolumnChoice]
if gameBoard[playerXrow][playerXcolumn] != "X" and gameBoard[playerXrow][playerXcolumn] != "O":
gameBoard[playerXrow, playerXcolumn] = "X"
else:
print("This spot is already taken.")
return gameBoard
在线:
gameBoard[playerOrow, playerOcolumn] = "X"
在这里,您需要定位一个特定的像这样的单元:在这种情况下,gameBoard [row] [column]:
gameBoard[playerOrow][playerOcolumn] = "X"
在更改之后我尝试运行代码,但又遇到了另一个错误,但我解决了。
正如我说的,我不知道是哪几行:
playerXcolumn = [eval(x) for x in playerXcolumnChoice]
playerXcolumn = [eval(x) for x in playerXcolumnChoice]
这样做是为了删除它们以及您要求用户输入的地方:
playerXrowChoice= input("Enter a row (0, 1, or 2) for player X: ")
playerXcolumnChoice= input("Enter a column (0, 1, or 2) for player X: ")
您需要将输入从字符串转换为int,以便您可以定位到GameBoard的正确单元格。