Python Forum
Tic Tac Toe, Python, code won`t run
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tic Tac Toe, Python, code won`t run
#1
Hi,

I am completely new to programming and am strugling with my assignment Tic Tac Toe in Python code. Can somebody please help me by pointing out my mistakes? I am sure there are lots of mistakes, but I am blind to them and how to solve them.

    def print_board(board):
        for row in board:
            print(row)
    
    
    def check_winner(board, current_player):
        if board[0][0] == board[0][1] == board[0][2] != "":
            return True
        elif board[1][0] == board[1][1] == board[1][2] !=  "":
            return True
        elif board[2][0] == board[2][1] == board[2][2] !=  "":
            return True
        elif board[0][0] == board[1][0] == board[2][0] != "":
            return True
        elif board[0][1] == board[1][1] == board[2][1] != "":
            return True
        elif board[0][2] == board[1][2] == board[2][2] != "":
            return True
        elif board[0][0] == board[1][1] == board[2][2] != "":
            return True
        elif board[2][0] == board[1][1] == board[0][2] != "":
            return True
        else:
            return False
        
    
    
    
    def update_board(board, row, col, current_player):
        board[row][col] = current_player
    
    
    
    def verify_entry(board, row, col):
    
        if board[row][col] == " ":
            return True
        else:
            return False
        
        if row in range(0,3) and col in range(0,3):
            return True
        else:
            return False
    
    
    
    board = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]]
    print_board(board)  # prints an empty board
    current = None
    moves = 0
    while moves < 9 and not check_winner(board, current):  # game over?
        # play
        current = "X" if (current == None or current == "O") else "O"
        row, col = map(int, input("Enter the move for " + current + ": ").split(","))
        while not verify_entry(board, row, col):
            print("Wrong entry. Think again!")
            row, col = map(int, input("Enter the move for " + current + ": ").split(","))
        update_board(board, row, col, current)
        print_board(board)  # prints the current board
        moves = moves + 1
    
    if moves < 9:
        print(current + " WON!")
Reply


Messages In This Thread
Tic Tac Toe, Python, code won`t run - by malinsimone - Sep-25-2020, 11:10 AM
RE: Tic Tac Toe, Python, code won`t run - by buran - Sep-25-2020, 11:24 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020