How to build a Tic Tac Toe Game in Python
In this tutorial, you will learn how to build the classic Tic Tac Toe game using Python and Visual Studio Code.
This tutorial will teach you how to build a simple Christmas guessing game using Python and Visual Studio Code.
Project goal | Get started with Python by creating a “Guess the reindeer” game |
---|---|
What you’ll learn | While loops, Python input() and randrange() functions |
Tools you’ll need | Python 3, Visual Studio Code and Python extension for Visual Studio Code |
Time needed to complete | 30 minutes |
You should have your Python development environment already set up:
If you need help with installing these tools, follow the instructions in the Microsoft Learn Module Set up your Python beginner development environment with Visual Studio Code.
Open Visual Studio Code and create a new file named guess-the-reindeer.py.
The general flow of the game will look like this:
Computer selects one of the 9 reindeer names (either Blixem, Comet, Cupid, Dancer, Dasher, Dunder, Prancer, Rudolph, or Vixen). Store the names of reindeers in a list named reindeers
.
|
|
This way, you only have to generate a random number in the range of 0 to 8 and then select the corresponding list item. Use the command
|
|
to generate a random number in the range of 0
(included) to len(reindeers)
(not included), where len(reindeers)
is the length of the list reindeers
(here the length is 9).
The randrange()
function returns a randomly selected number from a specified range. We can use two parameters to specify the lower and higher limit. For example, randrange(1, 10)
will return a number between 1
(included) and 10
(not included) and randrange(10)
will return a number between 0
(included) and 10
(not included).
Then use this number to select the corresponding list item:
|
|
and store the selected name in the variable reindeer_name
:
|
|
In order to use the randrange()
function you should add the following lines of code at the beginning of your program:
|
|
Create a variable called wrong_answers
to count the wrong player’s answers.
|
|
The player gets 3 chances to guess the correct name, so use a while
loop with a condition wrong_answers < 3
to repeat the guessing game 3 times.
|
|
while
loop you can repeat a set of instructions as long as a condition is true. You can also use the else
statement to run code when the condition becomes false.Use the else
keyword to print the name of the randomly selected reindeer at the end of the game.
|
|
Inside the while
loop, ask the player for a reindeer name using the input()
function.
|
|
input()
function. The input()
function has an optional parameter, known as prompt, which is a string that will be printed before the input.Use the method capitalize()
to convert only the first character to upper case.
|
|
The value stored in variable guess
(user input) must match the value stored in variable reindeer_name
(randomly selected name). Use an if-else
statement and print an appropriate message. If the player found the correct name, stop the while
loop using the break
statement, else increase the wrong_answers
value by 1.
|
|
Congratulations! You’ve created a “Guess the reindeer” game with Python! The last step is to ensure that your game works.
|
|
In this tutorial, you will learn how to build the classic Tic Tac Toe game using Python and Visual Studio Code.
This post will take you through the newest Read OCR API of Azure Computer Vision, which is used for extracting text from images.
In this post, you will explore the latest features of Azure Computer Vision and create a basic image analysis app.
In this article, you will deploy a function project to Azure using Visual Studio Code to create a serverless HTTP API.