Search code examples
python-3.xtic-tac-toe

"Can't assign to operator" error


I've been learning how to code for about a month now and I'm making a beginner's tic tac toe game using lists. I don't know why it's giving me this error. The part that's giving me the syntax error is "game_board[int(move) - 1] // 3 [int(move) - 1] % 3 = player_piece". Can someone please give me a simple fix a beginner can understand? I need to turn this in by 8am tomorrow.

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!")

Solution

  • You need to move the operations inside the list indices:

    game_board[(int(move) - 1) // 3][(int(move) - 1) % 3] = player_piece
    

    Right now, you have an opening square bracket after a number. This isn't allowed, which is why you get a syntax error.