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 >

Dubai, Dubai Coding and math, Dubai, UAE coding and math, Online Math Tutoring (US & Canadian Curriculum) For Expat Families

20 Apr 2026

SAT & AP math prep in Dubai: Virtual strategies, timelines and tutor packages

Dubai, Dubai Coding and math, Dubai, UAE coding and math, Online Math Tutoring (US & Canadian Curriculum) For Expat Families

20 Apr 2026

Dubai math tutor online: Live 1:1 US & Canadian curriculum tutoring for K–12 expat students

Dubai, Dubai Coding and math, Dubai, UAE coding and math, Online Math Tutoring (US & Canadian Curriculum) For Expat Families

20 Apr 2026

How We Match Ivy League & North American Tutors to Dubai Students — Credentials, Interviews, Results

Live Virtual Math Tutoring & Enrichment, Minneapolis

20 Apr 2026

Holiday & Weekend Bootcamps: Intensive Live Virtual Math Prep for Finals and AP/IB Exams (Minneapolis families)

Live Virtual Math Tutoring & Enrichment, Minneapolis

20 Apr 2026

Local Minneapolis Success Stories: Anonymous Case Studies of Virtual Math Tutoring Improving Grades and Confidence

Live Virtual Math Tutoring & Enrichment, Minneapolis

20 Apr 2026

Safety, vetting, and online-classroom best practices for Minneapolis parents choosing virtual math tutors

Live Virtual Math Tutoring & Enrichment, Minneapolis

20 Apr 2026

How much does live virtual math tutoring cost in Minneapolis? (Pricing guide + ROI for college‑track families)

Live Virtual Math Tutoring & Enrichment, Minneapolis

20 Apr 2026

How we measure success: tracking grades, AP/IB scores and mastery with live virtual math tutoring

Live Virtual Math Tutoring & Enrichment, Minneapolis

20 Apr 2026

Live virtual vs in‑person math tutoring for Minneapolis families: pros, cons and when to choose each