board = [" "] * 9 def mark(i): return board[i] if board[i] != " " else str(i + 1) def show(): print(" TIC TAC TOE") print() for row in range(3): a, b, c = mark(row*3), mark(row*3+1), mark(row*3+2) print(" " + a + " | " + b + " | " + c) if row < 2: print(" ---+---+---") print() def clear(): print("\n" * 30) WIN_LINES = [ [0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [2,4,6], ] player = "X" for turn in range(9): clear() show() cell = int(input("Player " + player + ", choose 1-9: ")) - 1 board[cell] = player won = False for line in WIN_LINES: if board[line[0]] == board[line[1]] == board[line[2]] != " ": clear() show() print(" Player " + player + " wins!") won = True break if won: break player = "O" if player == "X" else "X" else: clear() show() print(" It's a tie!")