Python Number Guessing Game for Kids
![]() |
| KidsKod original illustration. |
Can a computer keep a secret? Yes, and today your kid will teach it how! In this KidsKod project, you will build a Number Guessing Game in Python. The computer picks a secret number from 1 to 100, and the player keeps guessing while the computer says "Too high!" or "Too low!" until they find it. If you already made our Python Rock Paper Scissors Game for Kids, this is the perfect next step.
What you need
- A computer with a web browser
- A free online Python editor, such as online-python.com or Trinket, or Python with IDLE from python.org
- About 30 minutes
- A grown-up nearby if you are installing something for the first time
What you will learn
- How to make random numbers with the
randommodule - How to ask the player a question with
input()and turn the answer into a number withint() - How a
whileloop keeps the game going - How
if,elif, andelsehelp the computer decide what to say - How to count tries with a variable
Step 1: Let the computer pick a secret number
- Open your Python editor and make a new file called
guess.py. - Type these two lines:
import random secret = random.randint(1, 100) import randomgives Python its "dice box".random.randint(1, 100)picks a whole number from 1 to 100 and stores it in a variable calledsecret.
Step 2: Say hello to the player
- Add a friendly welcome message:
print("Welcome to the KidsKod Guessing Game!") print("I am thinking of a number from 1 to 100.") - Run the program. You should see your two messages appear.
Step 3: Make a variable to count tries
- Add this line:
tries = 0 - Every time the player guesses, we will add 1 to
tries. That way we can tell them how many guesses they needed.
Step 4: Use a loop to keep guessing
- Now add the heart of the game:
while True: guess = int(input("Your guess: ")) tries = tries + 1 while True:means "keep doing this again and again". The spaces at the start of the next lines are important: they tell Python which lines belong inside the loop.input()waits for the player to type, andint()turns their answer from text into a number.
Step 5: Give hints with if, elif, and else
- Still inside the loop (keep the same spaces!), add:
if guess > secret: print("Too high! Try a smaller number.") elif guess < secret: print("Too low! Try a bigger number.") else: print("You got it! The number was", secret) print("You needed", tries, "tries.") break breakstops the loop when the player wins, so the game ends.- Run the game and play a round. Can you find the number?
Step 6: The whole program
Here is the complete game in one piece, so you can check your work:
import random
secret = random.randint(1, 100)
print("Welcome to the KidsKod Guessing Game!")
print("I am thinking of a number from 1 to 100.")
tries = 0
while True:
guess = int(input("Your guess: "))
tries = tries + 1
if guess > secret:
print("Too high! Try a smaller number.")
elif guess < secret:
print("Too low! Try a bigger number.")
else:
print("You got it! The number was", secret)
print("You needed", tries, "tries.")
break
Step 7: Play smart (a secret strategy)
- Always start by guessing 50, right in the middle.
- If the computer says "Too high", guess halfway between 1 and 50 (about 25). If it says "Too low", guess about 75.
- Keep cutting the range in half. With this trick, you can always win in 7 tries or fewer! Real programmers call this binary search.
Wrap-up and challenge
Great job! You built a real Python game that uses random numbers, input, loops, conditions, and variables, the same building blocks used in big programs. Ready to level up?
- Challenge 1: Give the player only 7 tries. If they run out, print "Game over!" and show the secret number.
- Challenge 2: Let the player choose Easy (1–10), Medium (1–50), or Hard (1–100) at the start.
- Challenge 3: Ask "Play again? (yes/no)" at the end and start a new round if they say yes.
Want a mix of Scratch, Python, and Roblox ideas? Check out our Best Coding Projects for Kids in 2026. Happy coding from KidsKod!
