Your Child's First Python Program: A Number Guessing Game

Most first coding lessons ask a child to print "Hello, World!" and stop. That is a fine first line, but it is not a program a child wants to show anyone. A guessing game is different: it is a real, playable game in about 30 lines, and it teaches the four ideas that almost every program is built from — storing values, getting input, making decisions, and repeating until done.
This tutorial is written for a parent to sit through with a child aged about 10 and up. No experience is needed on either side. Everything here is free, and nothing here asks you to sign up for anything.
Why a guessing game first
A good first program has three qualities. First, it must run and do something visible within minutes, or a child concludes that coding is typing for its own sake. Second, it must contain a decision — if the guess is too high, say so — because decisions are what turn a list of instructions into a program. Third, it must be easy to change: a child who changes the range from 1–100 to 1–1000 and watches the game get harder has just done real programming, not just copying.
A guessing game has all three. It also has a natural ending — the game is won — which matters more than adults usually think. A child who finishes something wants to make it better. A child who never finishes just stops.
What you need (free, 5 minutes)
You need Python 3 on the computer. Python is free, and it is the language this tutorial uses. Download it from python.org/downloads and run the installer. On Windows, tick the small box that says "Add Python to PATH" before you click Install — that one box is the difference between Python working from anywhere and a confusing error later. On a Mac, the installer does the rest.
To write the code, use IDLE, which comes with Python — search for "IDLE" in the Start menu or Applications, open it, and choose File, then New File. That gives you a blank window where the game will live. There is no need to buy an editor or install anything else for this project.
To check the install, type this one line into IDLE's main window and press Enter:
print("hello")
If it prints hello underneath, Python is ready. If it does not, the most common cause on Windows is that the "Add Python to PATH" box was not ticked — reinstall with it ticked and try again.
Step 1: Say hello
Open a new file in IDLE and type this:
print("I'm thinking of a number between 1 and 100.")
print("Can you guess it?")
Save the file as guessing_game.py, then press F5 to run it. Two lines appear on the screen. That is the whole program so far, and it already teaches the first real idea: print() shows text, and the text goes inside the quotes. Change the words inside the quotes, run it again, and the output changes. That cause-and-effect — change the code, change the result — is the core of programming.
Step 2: Store a number in a variable
A variable is a named box that holds a value. Add this line at the top of the file, above the two print lines:
secret_number = 42
Now the program knows a number, but it never uses it. The name secret_number is your choice — Python does not care what you call it, but a human reading the code does. Good names are the cheapest form of documentation there is. Try changing 42 to another number and running the game: nothing visible changes yet, because nothing in the program reads the variable. That is normal, and it is worth noticing — a variable only matters when something uses it.
Step 3: Ask the player for a guess
Add these lines under the print lines:
guess = input("Your guess: ")
print("You guessed", guess)
Run it. The program stops and waits. That waiting is input() doing its job: it shows the prompt, waits for the player to type, and stores whatever was typed in guess. Type 50, press Enter, and the program prints it back.
There is a trap here that every beginner hits, so meet it now rather than later: input() always returns text, even when the player types digits. The text "50" and the number 50 look the same on screen but are different things to Python — you cannot compare text with a number using > or < and get a sensible answer. The fix is one word:
guess = int(input("Your guess: "))
int() converts the text into a real number. If the player types letters instead, the program will crash with a ValueError — that is expected, and the "Common errors" section below shows how to handle it once the game works.
Step 4: The full game
Now put it all together. Replace the whole file with this complete game, then run it with F5:
import random
secret_number = random.randint(1, 100)
tries = 0
print("I'm thinking of a number between 1 and 100.")
print("Can you guess it?")
while True:
guess = int(input("Your guess: "))
tries = tries + 1
if guess < secret_number:
print("Too low! Try again.")
elif guess > secret_number:
print("Too high! Try again.")
else:
print("You got it! The number was", secret_number)
print("It took you", tries, "tries.")
break
Play it twice. The second game is different from the first, because random.randint(1, 100) picks a new secret number each time the program runs. That single line is why the game is replayable — and it is also the first time the program does something the programmer did not decide in advance.
What each part of the game does
import random: loads Python's random-number tools. Without this line,random.randintdoes not exist.secret_number = random.randint(1, 100): picks a whole number from 1 to 100 and stores it in the variable.tries = 0: a counter, starting at zero. It goes up by one after every guess.while True:: a loop that repeats forever — until something inside it saysbreak. The loop is what makes the game keep asking instead of quitting after one guess.if / elif / else: the decision. Exactly one of the three branches runs each time: too low, too high, or correct.break: exits the loop the moment the player wins. Without it, the game would ask for another guess forever.
If your child understands those six bullets, they understand the skeleton of most programs: set up, loop, decide, stop. Everything else in programming is variations on that shape.
Five ways to make it harder
Do not move on to a new project yet. Changing this one teaches more than starting a new one. Pick one challenge, make it work, then pick another.
- Limit the guesses. Give the player 7 guesses. After the 7th wrong guess, print "Out of guesses — the number was ..." and stop. Hint: use
if tries == 7inside the loop, or changewhile Truetowhile tries < 7. - Change the range. Make it 1 to 1000. Notice how the game feels different — and how a good guessing strategy (always guess the middle of the remaining range) beats random guessing. That strategy has a name, binary search, and it is the same idea behind how computers find things fast.
- Add "very close". If the guess is within 5 of the secret number, print "Very close!" as well as "Too high" or "Too low". Hint:
abs(guess - secret_number) <= 5measures the distance. - Keep score across games. After a win, ask "Play again? (y/n)". If the answer is "y", pick a new secret number and reset the counter. Keep a running total of wins.
- Handle bad input. If the player types "banana", the game currently crashes. Wrap the input line in a
try / except ValueErrorblock so the game prints "Please type a number." and asks again instead of crashing. This is the single most useful error-handling pattern in Python, and it is worth learning on a game rather than on homework.
Common errors (and the fix)
IndentationError: Python cares about spaces at the start of a line. Everything inside thewhileloop must be indented by the same amount — four spaces is the standard. Mixed tabs and spaces cause this error; IDLE converts Tab to four spaces, so use Tab.NameError: name 'secret_number' is not defined: a typo in the variable name. Python treatssecret_numberandsecretNumberas two different names. Copy the name exactly.TypeError: '<' not supported between 'str' and 'int': theint()aroundinput()is missing, so the guess is still text. Add it back.- The game never ends: the
breakis missing or is indented under the wrong branch. It must sit inside theelseblock, at the same indent as the two print lines above it. SyntaxErroron theifline: check for the colon at the end of the line. Everyif,elif,else, andwhileline ends with a colon.
When an error appears, read the last line of the red message first — it names the error — then look at the line number it gives. Fixing errors is not a detour from coding. It is coding. Programmers spend a large part of their working day doing exactly this.
What your child just learned
In about 30 minutes and 30 lines, this game covers variables, input and output, type conversion, conditionals, loops, importing a library, and counting. That is most of a first month of a Python course, learned by building something playable rather than by reading about it. The next sensible step is not a bigger game — it is changing this one (the five challenges above), then a second small program such as a simple quiz or a rock-paper-scissors game, which reuses the same loop-and-decision shape with new rules.
One honest note: a single tutorial does not make a programmer, and no honest guide would claim otherwise. What it does is answer the only question that matters at the start — "can I actually build something?" — with a yes the child can see on the screen.
Frequently asked questions
What age is this right for?
About 10 and up for typing the code themselves. A younger child (8–9) can do it with a parent typing and the child deciding what to change — the decisions are the learning, not the typing.
Do we need to buy anything?
No. Python is free, IDLE comes with it, and this tutorial uses nothing else. If a site asks you to pay for "Python for kids" before you have written a single line, close it.
How long does it take?
About 30 minutes for the working game, including the install. The five challenges add another 30–60 minutes spread over a few days, which is a better pace than doing them all at once.
What if the game crashes?
That is normal and useful. Read the last line of the error message, check the line number, and compare that line with the code above character by character. Nine times out of ten it is a missing colon, a wrong indent, or a misspelled variable name.
What should we build next?
Rock-paper-scissors against the computer reuses the same ideas — random choice, input, if/elif/else — with a new rule set. After that, a quiz game that keeps score across questions. Both are small enough to finish in one sitting, which is the rule that matters most at this stage: finish things.
Sources
These are the official, free resources this guide draws on. Prices and program details change — always confirm on the program’s own site before enrolling.