"Can't assign to operator" 错误
"Can't assign to operator" error
我已经学习如何编码大约一个月了,我正在使用列表制作初学者的井字游戏。我不知道为什么它会给我这个错误。给我语法错误的部分是 "game_board[int(move) - 1] // 3 [int(move) - 1] % 3 = player_piece"。有人可以给我一个初学者可以理解的简单修复吗?我需要在明天早上 8 点之前把它交上来。
player_list = ['X', 'O']
player_num = 1
player_piece = player_list[player_num]
game_board = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
printTTT(game_board)
print()
while hasWinner(game_board) == False and movesLeft(game_board):
player_num = (player_num + 1) % 2
player_piece = player_list[player_num]
move = input("Please enter a valid move Player " + player_piece + ". ")
while moveValid(move, game_board) == False:
move = input("Not a valid move. Please enter a valid move. ")
game_board[int(move) - 1] // 3 [int(move) - 1] % 3 = player_piece
printTTT(game_board)
print()
if hasWinner(game_board) == True:
print("Congratulations! Player " + player_piece + " wins!!!")
else:
print("Tie game!")
您需要将操作移动到列表索引中:
game_board[(int(move) - 1) // 3][(int(move) - 1) % 3] = player_piece
现在,您在数字后有一个左方括号。这是不允许的,这就是您收到语法错误的原因。
我已经学习如何编码大约一个月了,我正在使用列表制作初学者的井字游戏。我不知道为什么它会给我这个错误。给我语法错误的部分是 "game_board[int(move) - 1] // 3 [int(move) - 1] % 3 = player_piece"。有人可以给我一个初学者可以理解的简单修复吗?我需要在明天早上 8 点之前把它交上来。
player_list = ['X', 'O']
player_num = 1
player_piece = player_list[player_num]
game_board = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
printTTT(game_board)
print()
while hasWinner(game_board) == False and movesLeft(game_board):
player_num = (player_num + 1) % 2
player_piece = player_list[player_num]
move = input("Please enter a valid move Player " + player_piece + ". ")
while moveValid(move, game_board) == False:
move = input("Not a valid move. Please enter a valid move. ")
game_board[int(move) - 1] // 3 [int(move) - 1] % 3 = player_piece
printTTT(game_board)
print()
if hasWinner(game_board) == True:
print("Congratulations! Player " + player_piece + " wins!!!")
else:
print("Tie game!")
您需要将操作移动到列表索引中:
game_board[(int(move) - 1) // 3][(int(move) - 1) % 3] = player_piece
现在,您在数字后有一个左方括号。这是不允许的,这就是您收到语法错误的原因。