All Articles

The Number Guessing Game

I find the “number guessing game” extremely versatile and useful for teaching Python to absolute newbies. Here is why.

The Number Guessing Game

Guess the number - A slot machine

The Number Guessing Game

When I try to introduce people without any programming background to Python, my first example is the number guessing game.

In my early days as a trainer, I questioned this choice quite often and tried to develop a more interesting, more vivid example. Over the years, however, I learned to appreciate it very much. As with many other things, it is not about the outcome itself but the evolvement of the concepts. Nowadays, you might call it story telling.

Here is the story I tell about the number guessing game.

The First Contact to Source Code

I start with a very simple form of number guessing, like so:

secret_number = "777"
user_guess = input()
print("Your guess was", user_guess)

This small example introduces the concept of input and output and shows the ability of a computer to store values in “named boxes” which we call variables.

So far, so good.

The “P” of the IPO Model

However, the programme is only useful if it “does something” with our input. Conceptually, I introduce the “process” part of the IPO model by adding a conditional block:

secret_number = "777"
user_guess = input()
print("Your guess was", user_guess)
if number_guess == user_guess:
    print("And your guess was correct!")
else:
    print("This was wrong, unfortunately.")

Data Types

As I stored the secret_number as a str, I don’t need an int conversion in the if-condition. If we do it now, we can introduce data types:

secret_number = 777
user_guess = input()
print("Your guess was", user_guess)
if number_guess == int(user_guess):
    print("And your guess was correct!")
else:
    print("This was wrong, unfortunately.")

Infinite Tries

The next part is to add a loop to the programme to enable infinite tries like so:

secret_number = 777
while True:
    user_guess = input()
    print("Your guess was", user_guess)
    if number_guess == int(user_guess):
        print("And your guess was correct!")
        break
    else:
        print("This was wrong, unfortunately.")

By now, we have introduced

  • variables
  • data types
  • conditions
  • loops

Pretty much for a small programme like that.

The Interesting Part

Now, the exciting part happens: What if we want to limit the number of tries to, say, 3. However, multiple entries of the same number should only count as one attempt. Of course we would need to introduce lists for that. And, some more ifs.

This part is fascinating for my curriculum for non-programmers, for example project managers. A supposedly simply formulated further requirement such as the limitation of attempts and the restriction that double inputs are not counted turns a very manageable programme into a relatively complex one from a beginner’s point of view.

Through this example, an understanding of the complexity that results from formal requirements already emerges in the first lesson of my introduction. I have often noted astonishment at how detailed one has to instruct the computer to do certain things. In the end, the programme looks like this:

secret_number = 777
guesses = []
while len(guesses) < 3:
    user_guess = input()
    if user_guess not in guesses:
        guesses.append(user_guess)
    print("Your guess was", user_guess)
    if number_guess == int(user_guess):
        print("And your guess was correct!")
        break
    else:
        print("This was wrong, unfortunately.")

Let’s take that programme apart:

  • We introduced lists (guesses = []) and we store each guess in that list (guesses.append(user_guess)). By that, we have - to put it very simply - tripled our storage requirements.
  • We have an additional condition in the while loop (while len(guesses) < 3) and a check if a number has been guessed before (if user_guess not in guesses:). In simple terms, we tripled our “function calls” needed for checking the input. So, again in great simplification, we increased the complexity three-fold.

Basically, this is precisely the part where it clicks with the participants of my courses: Really, everything has to be written down in detail in the source code for it to happen at all. Moreover, supposedly small changes that are intuitively added as a new “feature” can entail relatively complex considerations concerning the source code. At the same time, we have explained the essential elements of a programming language, such as variables, conditions, loops and lists in just a few lines.

In short: I love the Number Guessing Game as an introduction to programming.