DEV Community

Cover image for Create a Christmas guessing game with Python
Foteini Savvidou
Foteini Savvidou

Posted on • Updated on • Originally published at foteinisavvidou.codes

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 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

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 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.

reindeers = ["Blixem", "Comet", "Cupid", "Dancer", "Dasher", "Dunder", "Prancer", "Rudolph", "Vixen"]
Enter fullscreen mode Exit fullscreen mode

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

randrange(len(reindeers))
Enter fullscreen mode Exit fullscreen mode

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:

reindeers[randrange(len(reindeers))]
Enter fullscreen mode Exit fullscreen mode

and store the selected name in the variable reindeer_name:

reindeer_name = reindeers[randrange(len(reindeers))]
Enter fullscreen mode Exit fullscreen mode

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

from random import seed
from random import randrange
from datetime import datetime

seed(datetime.now())
Enter fullscreen mode Exit fullscreen mode

The player gets 3 chances to guess the name

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

wrong_answers = 0
Enter fullscreen mode Exit fullscreen mode

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 wrong_answers < 3:
    pass
Enter fullscreen mode Exit fullscreen mode

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.

while wrong_answers < 3:
    pass
else:
    print("It's " + reindeer_name)
Enter fullscreen mode Exit fullscreen mode

Get user input

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

guess = input("What is the name of the reindeer? ")
Enter fullscreen mode Exit fullscreen mode

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.

guess = input("What is the name of the reindeer? ").capitalize()
Enter fullscreen mode Exit fullscreen mode

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.

if guess == reindeer_name:
    print("That's right! It's " + guess)
    print("Merry Christmas!")
    break
else:
    print("That's wrong!")
    wrong_answers += 1
Enter fullscreen mode Exit fullscreen mode

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

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)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)