Build a Number Guessing Game in Python (Kid-Friendly Walkthrough)

ยท 3 min read ยท By the Tiny Coderz team

The number guessing game is a classic first Python project, and for good reason. It uses variables, input, loops and if statements in a small program a child can finish in one sitting and then show off to the family.

The computer picks a secret number. The player keeps guessing. After each guess, the computer says "too high" or "too low" until they get it.

Step 1: pick a secret number

Python can choose random numbers if we ask for the random tool:

import random

secret = random.randint(1, 20)

import random brings in the tool. randint(1, 20) picks a whole number from 1 to 20, including both ends.

Step 2: ask for a guess

guess = int(input("Guess my number from 1 to 20: "))

Remember that input gives back text, so int(...) converts it into a number we can compare.

Step 3: check the guess

if guess == secret:
    print("You got it!")
elif guess < secret:
    print("Too low.")
else:
    print("Too high.")

Notice the double equals ==. A single = stores a value; a double == asks "are these equal?" Mixing them up is one of the most common beginner bugs, covered in common Python mistakes kids make.

Step 4: keep going with a loop

Right now the game ends after one guess. A while loop fixes that:

import random

secret = random.randint(1, 20)
guess = 0
tries = 0

while guess != secret:
    guess = int(input("Guess my number from 1 to 20: "))
    tries = tries + 1
    if guess < secret:
        print("Too low.")
    elif guess > secret:
        print("Too high.")

print("You got it in", tries, "tries!")

The loop repeats as long as guess is not equal to secret. We start guess at 0 so the loop begins. Once the player is right, the loop stops and the winning message prints.

Ways to make it yours

Once it works, the fun begins. Each of these is a small, safe experiment:

  • Change the range to 1 to 100 and see how the game feels.
  • Add a limit: the player only gets 6 tries. (Hint: add and tries < 6 to the loop condition.)
  • Add a name: ask who is playing and use it in the messages.
  • Make it friendlier: say "Very close!" if the guess is within 2 of the secret. You will need abs(guess - secret).
  • Play again: wrap the whole game in another loop that asks "Play again?"

What your child is really learning

Notice how many ideas are tucked into this tiny game: variables, conversion, comparison, branching and repetition. A good way to deepen it is to ask, "What is the smartest first guess?" Most children discover that guessing the middle each time finds the answer fastest, which is the beginning of understanding how real search algorithms work.

If the program crashes, treat it as a puzzle, not a failure. Our guide on debugging for kids has a simple routine for tracking down problems. For more ideas like this one, see Python projects for beginners.

Try Python Fundamentals with your child

Every lesson in Python Fundamentals opens with an animated explanation, then a short practice. You can read a full sample lesson with no account.

Keep reading