Create a Christmas guessing game with Python

This tutorial will teach you how to build a simple Christmas guessing game using Python and Visual Studio Code.

Project goalGet started with Python by creating a “Guess the reindeer” game
What you’ll learnWhile loops, Python input() and randrange() functions
Tools you’ll needPython 3, Visual Studio Code and Python extension for Visual Studio Code
Time needed to complete30 minutes

Prerequisite

You should have your Python development environment already set up:

  • Install Python 3
  • Install Visual Studio Code or other code editor

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.

Instructions

Open Visual Studio Code and create a new file named guess-the-reindeer.py.

Creating the guessing game

The general flow of the game will look like this:

  • Computer selects one reindeer.
  • The player has three chances to guess the name of the reindeer.
    • If the player’s guess is wrong, the message “That’s wrong!” is displayed.
    • If the player’s guess is right, a successful message is displayed.
  • Once the game has finished, the name of the reindeer is revealed.

Choose a random reindeer name

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.

1
reindeers = ["Blixem", "Comet", "Cupid", "Dancer", "Dasher", "Dunder", "Prancer", "Rudolph", "Vixen"]

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

1
randrange(len(reindeers))

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:

1
reindeers[randrange(len(reindeers))]

and store the selected name in the variable reindeer_name:

1
reindeer_name = reindeers[randrange(len(reindeers))]

In order to use the randrange() function you should add the following lines of code at the beginning of your program:

1
2
3
4
from random import seed
from random import randrange
from datetime import datetime
seed(datetime.now())

The player gets 3 chances to guess the name

Create a variable called wrong_answers to count the wrong player’s answers.

1
wrong_answers = 0

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.

1
2
while wrong_answers < 3:
    pass
With the 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.

1
2
3
4
while wrong_answers < 3:
    pass
else:
    print("It's " + reindeer_name)

Get user input

Inside the while loop, ask the player for a reindeer name using the input() function.

1
guess = input("What is the name of the reindeer? ")
To receive information from the user through the keyboard, you can use the 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.

1
guess = input("What is the name of the reindeer? ").capitalize()

Check if the player found the correct name

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.

1
2
3
4
5
6
7
if guess == reindeer_name:
    print("That's right! It's " + guess)
    print("Merry Christmas!")
    break
else:
    print("That's wrong!")
    wrong_answers += 1

Congratulations! You’ve created a “Guess the reindeer” game with Python! The last step is to ensure that your game works.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
from random import seed
from random import randrange
from datetime import datetime
seed(datetime.now())
reindeers = ["Blixem", "Comet", "Cupid", "Dancer", "Dasher", "Dunder", "Prancer", "Rudolph", "Vixen"]
print("=== Guess the Santa's Reindeer Name Game ===")
reindeer_name = reindeers[randrange(len(reindeers))]
wrong_answers = 0
while wrong_answers < 3:
    guess = input("What is the name of the reindeer? ").capitalize()
    if guess == reindeer_name:
        print("That's right! It's " + guess)
        print("Merry Christmas!")
        break
    else:
        print("That's wrong!")
        wrong_answers += 1
else:
    print("It's " + reindeer_name)

You May Also Like