As you can probably tell, I'm very new to Python and so while my code "works", it also randomly breaks. I tried using ChatGPT in this case but it's not very helpful. I would really appreciate the help!
from IPython.display import clear_outputdef display_board(board): clear_output() print(board[0] +"|" + board[1] +"|" + board[2]) print("------") print(board[3] +"|" + board[4] +"|" + board[5]) print("------") print(board[6] +"|" + board[7] +"|" + board[8])def player_input(first_move): marker1 = " " marker2 = " " while marker1 not in ['X','O']: if first_move == 1: marker1 = input("Player 1, What would you like to pick? X or O: ") if marker1 not in ['X','O']: print("Sorry, that is not a valid input! Try again") else: if marker1 == 'X': marker2 = 'O' print("Player 1 is X and Player 2 is O") elif marker1 == 'O': marker2 = 'X' print("Player 1 is O and Player 2 is X") else: marker2 = input("Player 2, What would you like to pick? X or O: ") if marker2 not in ['X','O']: print("Sorry, that is not a valid input! Try again") else: if marker2 == 'X': marker1 = 'O' print("Player 2 is X and Player 1 is O") elif marker2 == 'O': marker1 = 'X' print("Player 2 is O and Player 1 is X") return (marker1, marker2)def place_marker(board, marker): position = int(input("Where would you like to place the marker? (1 - 9): ")) if position == 1: board[6] = marker elif position == 2: board[7] = marker elif position == 3: board[8] = marker elif position == 4: board[3] = marker elif position == 5: board[4] = marker elif position == 6: board[5] = marker elif position == 7: board[0] = marker elif position == 8: board[1] = marker elif position == 9: board[2] = marker else: print("Invalid input") display_board(board)def win_check(board, mark): if (board[0] == board[1] == board[2] == mark) or (board[3] == board[4] == board[5] == mark) or (board[6] == board[7] == board[8] == mark) or (board[0] == board[3] == board[6] == mark) or (board[1] == board[4] == board[7] == mark) or (board[2] == board[5] == board[8] == mark) or (board[0] == board[4] == board[8] == mark) or (board[2] == board[4] == board[6] == mark): return Trueimport randomdef choose_first(): first_move = random.randint(1,2) if first_move == 1: print("Player 1 (Left) will go first!") return first_move else: print("Player 2 (Right) will go first!") return first_movedef space_check(board, position): if board[0] == " " or board[1] == " " or board[2] == " " or board[3] == " " or board[4] == " " or board[5] == " " or board[6] == " " or board[7] == " " or board[8] == " ": return Truedef full_board_check(board): if board[0] != " " and board[1] != " " and board[2] != " " and board[3] != " " and board[4] != " " and board[5] != " " and board[6] != " " and board[7] != " " and board[8] != " ": full_board = 1 else: full_board = 0 return full_boarddef player_choice(board): if space_check() == True: position = input("What is your next position? ") return positiondef replay(): play_again = '' while play_again not in ['Y','N']: play_again = input("Would you like to play again? Y or N: ") if play_again not in ["Y","N"]: print("Sorry, not a valid input") elif play_again == 'Y': game() else: print("Thank you for playing!")def win_check(board, marker): win_counter = 0 if (board[0] == board[1] == board[2] == marker) or (board[3] == board[4] == board[5] == marker) or (board[6] == board[7] == board[8] == marker) or (board[0] == board[3] == board[6] == marker) or (board[1] == board[4] == board[7] == marker) or (board[2] == board[5] == board[8] == marker) or (board[0] == board[4] == board[8] == marker) or (board[2] == board[4] == board[6] == marker): win_counter = 1 return win_counterdef game(): tie_game = 1 board = [" "," "," "," "," "," "," "," "," "] display_board(board) print("Welcome to Tic Tac Toe") #Choose with player will go first first_move = choose_first() #Ask player1 if they want to be X or O? marker1, marker2 = player_input(first_move) full_board = full_board_check(board) #start game while full_board == 0: if first_move == 1: full_board = full_board_check(board) place_marker(board,marker1) #place_marker(board,marker2) player1win = win_check(board, marker1) player2win = win_check(board, marker2) if player1win == 1: print("Player 1 has won!") break elif player2win == 1: print("Player 2 has won!") break elif full_board_check(board) == 1: print("Game has tied!") break place_marker(board,marker2) full_board = full_board_check(board) if first_move == 2: full_board = full_board_check(board) place_marker(board,marker2) #place_marker(board,marker2) player1win = win_check(board, marker1) player2win = win_check(board, marker2) if player1win == 1: print("Player 1 has won!") break elif player2win == 1: print("Player 2 has won!") break elif full_board_check(board) == 1: print("Game has tied!") break place_marker(board,marker1) full_board = full_board_check(board) game()replay()I tried playing around but it still randomly crashes! It definitely works and I've played it multiple times but it unexpectantly breaks and I have to restart the code. I couldn't also figure out when it breaks and it seems very random to me at first.