Exercise: 21 Matches

Andrew Lombardi
Nov 6 · 2 min read

In order to practice your skills with Python, an exercise is in order. If you haven’t yet set up your environment, read through this simple tutorial before starting this exercise.

Game Rules

There are 2 players. They have 21 matches. At each players turn, they can pick 1, 2 or 3 matches. The winner will have chosen the number of matches which means that the other player will be left with a single match left.

In order to test skills, we’re going to write this 3 different ways, with each increasing a bit of the complexity.

Two player version

  1. Create a Python program that can be run from the command line / terminal
  2. This program will be a two player game
  3. It will show prompts each time someone chooses a number of matches
  4. It should validate that the user has chosen a valid number of matches. If there are 2 matches left and the user chooses 3, it should show an error and ask them to try again. If the user tries to choose a number other than 1, 2 or 3 it should also show an error.
  5. It should display a notification if someone wins the game and exit

One player version

There’s an algorithm that determines who wins or not. Can you figure it out? You’ll need to so you can create a computer version that always wins.

  1. Copy the two player version, and instead of asking a second player for their selection, you’re going to write a function that acts as a player and determines the appropriate number of matches so that they always win

Computer goes first

Switch it up and let the computer take the first turn. What happens when it has first crack?

New rules and alternatives

Let’s get creative and think through different changes and how we can take the concept of 21 matches and put our own spin on it.

Concepts and Skills

In order to program the above, you’ll need to know a few things with Python that you may not have done before.

Conditionals

In order to tell the computer to follow a specific execution path, we may need to use a conditional. With Python, that’s the if, elsif, else

For example, if you have a variable foo and you wanted to print bar if the variable foo had a value of “yes” you could write:

if foo is 'yes':
print('bar')

Remember two things, 1. Python uses a colon to denote a different block of code and 2. Python loves tabs, so when you’re in a different block of code, you’ll want to tab in

User input

Let’s take a simple example, in Python 3 to take user input you’ll do the following:

foo = input("What does the fox say?")

And whatever the user put into the computer after that, will be contained in the variable foo.

Andrew Lombardi

Written by

international speaker, father, entrepreneur, java enterprise developer, to our success!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade