Python Kids – Learn to Code your first game!

Singapore math classes

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 https://replit.com/.
  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("|".join(row))
        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:
        try:
            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
                break
            else:
                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:
        display_board(board)
        get_input(board, player)
        player = "O" if player == "X" else "X"
        moves += 1

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

if __name__ == "__main__":
    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!

SHARE WITH FRIENDS >

After-school Coding & Game Design Classes (Godot), Ottawa, Tutorials

20 Apr 2026

Godot Game Design for Teens in Ottawa: Find After‑School Classes + a Step‑by‑Step 2D Game Tutorial

After-school Live Virtual Scratch & Block Coding Classes For Kids, Phoenix

20 Apr 2026

Weekend Workshops & Summer Coding Camps for Phoenix Kids: Short Intensives in Scratch & Block Coding

After-school Live Virtual Scratch & Block Coding Classes For Kids, Phoenix

20 Apr 2026

Tech Requirements & Onboarding for Live Online Scratch Classes (Phoenix Families’ Checklist)

After-school Live Virtual Scratch & Block Coding Classes For Kids, Phoenix

20 Apr 2026

Hybrid & School‑Partnership After‑School Coding in the Phoenix Metro: Options for Scottsdale, Tempe, Chandler & Gilbert

After-school Live Virtual Scratch & Block Coding Classes For Kids, Phoenix

20 Apr 2026

How We Teach Scratch & Block Coding by Age: Elementary vs. Middle School Curriculum (Phoenix metro)

After-school Live Virtual Scratch & Block Coding Classes For Kids, Phoenix

20 Apr 2026

Instructor Qualifications & Safety for Live Virtual Scratch & Block Coding Classes — Phoenix Families’ Guide

After-school Live Virtual Scratch & Block Coding Classes For Kids, Phoenix

20 Apr 2026

Are live virtual Scratch classes effective for young learners? Evidence, best practices & Phoenix parent tips

After-school Live Virtual Scratch & Block Coding Classes For Kids, Phoenix

20 Apr 2026

Free trial & demos: How to book a live virtual Scratch class for kids in Phoenix

After-school Live Virtual Scratch & Block Coding Classes For Kids, Phoenix

20 Apr 2026

Pricing and packages for kids’ block coding classes in Phoenix: sibling discounts, make-ups & payment plans