Python for Kids: Build a Simple Number Guessing Game

Must read

BRAINYBLOOMCLUB
BRAINYBLOOMCLUBhttps://brainybloomclub.com
Helping Adults Raise Tomorrow’s Innovators. Practical STEM projects, learning roadmaps, product reviews and AI safety guidance for parents, guardians and educators.

This Python for kids tutorial helps a beginner build a number guessing game with support from a parent or teacher. You will practise displaying text, storing values, making decisions and repeating actions. Start with one line, then build towards a game that handles mistakes without crashing. The goal is understanding, not finishing against a stopwatch.

Python for Kids: What You Need

  • A computer with Python 3 and an editor that supports keyboard input. An adult can help install Python from the official download page; IDLE is included with standard desktop installations.
  • Alternatively, use a school-approved online editor that supports input(). Adults should check accounts, privacy, age requirements and any charges before use.
  • A new file named guessing_game.py. In IDLE, open a new file, save it, and use Run → Run Module.

Type code into the file editor, not directly into a web search box. Keep indentation consistent: four spaces at each level. Save a working copy before adding a new feature.

Your First Line of Code

Type this into the editor and click “Run”:

print("Hello, World!")

You should see Hello, World! appear on the screen. Congratulations — you just wrote your first Python program. The print() function tells Python to display text on the screen. The text inside the quotation marks is called a string.

Variables: Storing Information

Variables are containers that store information. Think of them as labelled boxes:

name = "Alex"
age = 12
print("My name is " + name)
print("I am", age, "years old")

Here, name stores a text string and age stores a number. You can use variables anywhere in your code, and you can change their values at any time.

Input: Getting Information from the User

The input() function asks the user to type something:

name = input("Choose a made-up player name: ")
print("Nice to meet you, " + name + "!")

When you run this, the program pauses and waits for you to type your name. After you press Enter, it greets you personally. This is how programs become interactive.

Conditionals: Making Decisions

An if statement chooses what to do. This fixed example is useful before adding input:

guess = 7
secret = 10

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

Run it with 7, then change guess to 12 and finally 10. Predict the output before each run. The elif branch means “otherwise, if”; else handles the remaining case. A colon ends each condition line, and the four-space indentation groups the instructions beneath it. See the official Python control-flow tutorial for the language rules.

Loops: Repeating Actions

Loops run code multiple times. A while loop repeats as long as a condition is true:

count = 1
while count <= 5:
    print("Count is:", count)
    count = count + 1
print("Done!")

This prints the numbers 1 through 5, then stops. Loops are essential for games, animations and any program that needs to repeat actions.

Build It: Number Guessing Game

The computer picks an integer from 1 to 20, including both endpoints. Copy the complete program into a fresh file:

import random

secret = random.randint(1, 20)
guesses = 0
print("I am thinking of a number between 1 and 20.")

while True:
    answer = input("Your guess (or q to quit): ").strip()
    if answer.lower() == "q":
        print("Thanks for playing!")
        break
    try:
        guess = int(answer)
    except ValueError:
        print("Please enter a whole number.")
        continue
    if not 1 <= guess <= 20:
        print("Choose a number from 1 to 20.")
        continue
    guesses = guesses + 1
    if guess < secret:
        print("Too low! Try again.")
    elif guess > secret:
        print("Too high! Try again.")
    else:
        print("You got it in", guesses, "guesses!")
        break

input() returns text. int() attempts to convert it to an integer, while try and except ValueError handle inputs such as “hello” or “2.5”. continue restarts the loop without counting an invalid guess; break exits after success or quitting. The range check also rejects 0 and 21.

Guessing the middle of the remaining interval is a useful strategy. After a “too low” hint, discard that guess and all lower numbers. After “too high”, discard that guess and all higher numbers. Explain your remaining range aloud rather than guessing randomly.

What You Learned

  • print() — display text on screen.
  • Variables — store and reuse information.
  • input() — get information from the user.
  • Conditionals (if/elif/else) — make decisions.
  • Loops (while) — repeat actions.
  • import — use built-in Python libraries like random.

Challenges to Try Next

  • Add a maximum number of guesses (for example, 5) and a “Game Over” message.
  • Let the player choose the difficulty (1-10, 1-50 or 1-100).
  • Keep a running score across multiple rounds.
  • Build a different game: a password generator, a quiz or a story generator.

Test Your Game Like a Programmer

Temporarily replace the random-number line with secret = 10. Enter 5, 15 and 10: expect “too low”, “too high” and success in three guesses. Restart and enter “hello”, 0 and 21 before 10: the successful guess should count as one. Try q to check that quitting works. Restore the random line afterwards.

If you see a syntax error, check colons, paired quotation marks and indentation. A program that appears stuck may simply be waiting for input in its console. Change one thing at a time, rerun the same test and write down what changed. Testing is part of building, not a sign that you are bad at coding.

For a classroom pair, let one child type while the other predicts the next output; swap roles regularly. A learner who finds typing tiring can edit just the range or message first. Keep early modifications small enough that the child can explain them without reading a copied solution.

Explore our Coding hub for more programming activities and our Parents hub for guidance on supporting young learners.

- Advertisement -spot_img

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -spot_img

Latest article