Tic Tac Toe Game in Python for Beginners

Esteban Thilliez
Tech Tavern
Published in
6 min readAug 6, 2022

--

Tic Tac Toe

In programming, the best way to learn is to practice. I will give you a chance of practicing through a little exercise: a Tic Tac Toe game.

Not a Medium subscriber? Read for free here!

Prerequisites

To complete this exercise, you need to know Python basics such as lists, conditions, loops, and functions.

Statement

  • We’re building a console Tic Tac Toe game playable against a computer
  • The board is a 3x3 grid numbered from 1 to 9 so that we can ask the player a cell to play by number.
  • There is no A.I., the computer is playing random.

Tips

  • We can modelize the board with a list of characters which can be either “X”, “O” or an empty string.
  • To generate random numbers, we import Python’s random package and we use the randint(a, b)function which generates an int between a and b.
import randomrandom.randint(1, 10)
# 3
  • The game should be played this way :
  1. Board drawing.
  2. The player is asked which square he wants to play.
  3. Update the board according to the chosen square.

--

--

Esteban Thilliez
Tech Tavern

I’m Esteban, and I enjoy writing about programming, trading, productivity, knowledge management, books, etc! esteban-thilliez.com / contact@esteban-thilliez.com