Python Kids – Learn to Code your first game!

In this tutorial, we will create a simple game of Tic Tac Toe in Python using the Replit IDE. Replit is a free online code editor and compiler that supports many programming languages, including Python. Let’s begin by setting up our environment and writing the game’s basic structure.

Step 1: Setting up the Replit environment

  1. Go to
  2. Sign up for a free account if you don’t have one.
  3. Click on “Create” in the top right corner and choose “Python” as the language.
  4. Name your project “TicTacToe” and click “Create Repl”.

Step 2: Designing the game board

We’ll represent the Tic Tac Toe board as a 3×3 list of strings, where each element is either an “X”, an “O”, or an empty space.

def create_board():
    return [[" " for _ in range(3)] for _ in range(3)]

def display_board(board):
    for row in board:
        print("-" * 5)

Step 3: Checking for a win.

We need a function that checks if there’s a winner in the current game state. We’ll check for a win in rows, columns, and diagonals.

def check_win(board):
    for row in board:
        if row[0] == row[1] == row[2] != " ":
            return True

    for col in range(3):
        if board[0][col] == board[1][col] == board[2][col] != " ":
            return True

    if board[0][0] == board[1][1] == board[2][2] != " " or board[0][2] == board[1][1] == board[2][0] != " ":
        return True

    return False

Step 4: Handling player input.

We need to get the user’s input for the row and column they’d like to place their symbol in. We’ll validate the input and update the board accordingly.

def get_input(board, player):
    while True:
            row = int(input(f"Player {player}, enter the row (0-2): "))
            col = int(input(f"Player {player}, enter the column (0-2): "))
            if board[row][col] == " ":
                board[row][col] = player
                print("That spot is already taken. Try again.")
        except (ValueError, IndexError):
            print("Invalid input. Please enter a number between 0 and 2.")

Step 5: Main game loop.

Now we’ll create the main game loop, which will alternate between players until there’s a winner or the board is full.

def main():
    board = create_board()
    player = "X"
    moves = 0

    while not check_win(board) and moves < 9:
        get_input(board, player)
        player = "O" if player == "X" else "X"
        moves += 1

    if check_win(board):
        print(f"Player {player} wins!")
        print("It's a draw!")

if __name__ == "__main__":

That’s it! You’ve successfully created a simple Tic Tac Toe game in Python using the Replit IDE. To play the game, simply click the “Run” button at the top of the Replit window. Enjoy playing Tic Tac Toe with a friend or challenge yourself to improve the game by adding features such as:

  1. Improved input validation: Ensure that users can only enter valid input (i.e., numbers within the correct range and non-empty cells).
  2. Enhanced user interface: Create a more visually appealing game board or add colors to make the game more engaging.
  3. AI opponent: Implement a simple AI opponent that can play against the user, making the game more challenging and fun.
  4. Player customization: Allow users to choose their symbols or play with custom symbols.
  5. Score tracking: Keep track of wins, losses, and draws for each player during multiple rounds of the game.

To further improve your Python skills, consider exploring other game projects or applying what you’ve learned here to create entirely new applications. Good luck, and happy coding!

Leave a Comment