1

There is a code snippet turnset = False in line 14 that displayed as invalid syntax but an exact replica is in line 5 and works fine! Code here:

X = "X"
O = "O"
w = "none"
board = [N, N, N, N, N, N, N, N, N]
win = False
turn = input("Player 1 first?")
p1 = input("Player 1:")
p2 = input("Player 2:")
print(" ")
for turns in range (9
 if (turn)
  turn = False
  print(p1 + "'s turn!")
  x = input("X coordinates:")
  y = input("Y coordinates:")
  pos = (y - 1) * 3 + x - 1
  board[pos] = X
 else:
  turn = True
  if False: 
   print(p2 + "'s turn!")
   x = input("X coordinates:")
   y = input("Y coordinates:")
   pos = (y - 1) * 3 + x - 1
   board[pos] = O
  print(board[0] + board[1] + board[2])
  print(board[3] + board[4] + board[5])
  print(board[6] + board[7] + board[8])
  tile = X
  if ((board[0] == tile and board[1] == tile and board[2] == tile) or (board[3] == tile and board[4] == tile and board[5] == tile) or (board[6] == tile and board[7] == tile and board[8] == tile) or ((board[0] == tile and board[3] == tile and board[6] == tile) or (board[1] == tile and board[4] == tile and board[7] == tile) or (board[2] == tile and board[5] == tile and board[8] == tile) or ((board[0] == tile and board[4] == tile and board[8] == tile) or (board[2] == tile and board[4] == tile and board[6] == tile))
   w = p1
  
   break
  tile = O
  if ((board[0] == tile and board[1] == tile and board[2] == tile) or (board[3] == tile and board[4] == tile and board[5] == tile) or (board[6] == tile and board[7] == tile and board[8] == tile) or ((board[0] == tile and board[3] == tile and board[6] == tile) or (board[1] == tile and board[4] == tile and board[7] == tile) or (board[2] == tile and board[5] == tile and board[8] == tile) or ((board[0] == tile and board[4] == tile and board[8] == tile) or (board[2] == tile and board[4] == tile and board[6] == tile))
   w = p2
  break
 if (w != "none")
  print(w + "won!")
 else
  print("Tie!")
2
  • Hint: What is the difference between the code that came before line 5 and the code that came before line 14? Commented Jul 14, 2021 at 12:36
  • 2
    Are you certain that for turns in range (9 is correct? Hint #2: it's not. Commented Jul 14, 2021 at 12:38

1 Answer 1

1

Every indented code block must be following a colon (:) at the end of the previous line, so the for loop and the if statements must end with a colon. When a line gets too long, you can add linebreaks like so (and in doing so to your code I found some bracket inconsistencies that I fixed below):

X = "X"
O = "O"
w = "none"
board = [N, N, N, N, N, N, N, N, N]
win = False
turn = input("Player 1 first?")
p1 = input("Player 1:")
p2 = input("Player 2:")
print(" ")
for turns in range(9):
 if (turn):
  turn = False
  print(p1 + "'s turn!")
  x = input("X coordinates:")
  y = input("Y coordinates:")
  pos = (y - 1) * 3 + x - 1
  board[pos] = X
 else:
  turn = True
  if False: 
   print(p2 + "'s turn!")
   x = input("X coordinates:")
   y = input("Y coordinates:")
   pos = (y - 1) * 3 + x - 1
   board[pos] = O
  print(board[0] + board[1] + board[2])
  print(board[3] + board[4] + board[5])
  print(board[6] + board[7] + board[8])
  tile = X
  if ((board[0] == tile and board[1] == tile and board[2] == tile) or \
      (board[3] == tile and board[4] == tile and board[5] == tile) or \
      (board[6] == tile and board[7] == tile and board[8] == tile) or \
      (board[0] == tile and board[3] == tile and board[6] == tile) or \
      (board[1] == tile and board[4] == tile and board[7] == tile) or \
      (board[2] == tile and board[5] == tile and board[8] == tile) or \
      (board[0] == tile and board[4] == tile and board[8] == tile) or \
      (board[2] == tile and board[4] == tile and board[6] == tile)):
   w = p1
  
   break
  tile = O
  if ((board[0] == tile and board[1] == tile and board[2] == tile) or \
      (board[3] == tile and board[4] == tile and board[5] == tile) or \
      (board[6] == tile and board[7] == tile and board[8] == tile) or \
      (board[0] == tile and board[3] == tile and board[6] == tile) or \
      (board[1] == tile and board[4] == tile and board[7] == tile) or \
      (board[2] == tile and board[5] == tile and board[8] == tile) or \
      (board[0] == tile and board[4] == tile and board[8] == tile) or \
      (board[2] == tile and board[4] == tile and board[6] == tile)):
   w = p2
  break
 if (w != "none"):
  print(w + "won!")
 else:
  print("Tie!")

Finally, note that it's according to PEP-8 to use 4 spaces as the indentation rather than 1.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.