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 >

Live Virtual Math Tutoring & Enrichment, Minneapolis

20 Apr 2026

Top Live Virtual Math Enrichment Classes and Small Groups for Minneapolis Teens (STEM Track & Competition Prep)

Live Virtual Math Tutoring & Enrichment, Minneapolis

20 Apr 2026

How to choose an Ivy League or top‑STEM‑university live virtual math tutor in Minneapolis

Live Virtual Math Tutoring & Enrichment, Minneapolis

20 Apr 2026

Evening and Weekend Live Virtual Math Tutoring: After‑School Scheduling Tips for Minneapolis Parents

Live Virtual Math Tutoring & Enrichment, Minneapolis

20 Apr 2026

Can live virtual tutoring improve AP Calculus & AP Statistics scores? A Minneapolis parent’s guide

Live Virtual Coding & Math Enrichment (K–12), Tampa

20 Apr 2026

Affordable Virtual Math Tutoring Options for Busy Tampa Families (Evenings & Weekends)

Live Virtual Coding & Math Enrichment (K–12), Tampa

20 Apr 2026

Summer Virtual Coding Camps for Tampa Bay Kids: Dates, Ages, and What They Build

Live Virtual Coding & Math Enrichment (K–12), Tampa

20 Apr 2026

How Live Virtual Small-Group Classes Improve Math Confidence: Evidence, Metrics, and Tampa Parent Stories

Live Virtual Coding & Math Enrichment (K–12), Tampa

20 Apr 2026

What Tampa Parents Should Ask About Instructor Credentials, Class Size, and Outcomes

Live Virtual Coding & Math Enrichment (K–12), Tampa

20 Apr 2026

Project-Based Coding for Kids: Portfolio Projects That Help Tampa Students Stand Out for High School & College