Python is one of the easiest and most powerful programming languages for kids to start learning. One of the most fun ways to practice Python is by creating a simple game. Games not only make coding exciting but also help kids build problem-solving and logical thinking skills.
In this blog, we’ll create a “Guess the Number” game step by step.
What is the Game About?
The computer will “think” of a number, and the player (your child!) has to guess it. The computer will give hints like “too high” or “too low” until the correct number is guessed.
Python Code for Guess the Number Game
Here’s a simple version of the game:
import random
# Computer chooses a number between 1 and 20
secret_number = random.randint(1, 20)
print("Welcome to Guess the Number!")
print("I am thinking of a number between 1 and 20.")
# Let’s give the player 5 chances to guess
for i in range(5):
guess = int(input("Take a guess: "))
if guess < secret_number:
print("Too low! Try again.")
elif guess > secret_number:
print("Too high! Try again.")
else:
print("Congratulations! You guessed it right!")
break
if guess != secret_number:
print("Sorry, the number was {secret_number}. Better luck next time!")
How It Works
- Random Number: Python randomly picks a number between 1 and 20.
- User Guess: The player enters a number.
- Hints: The computer tells whether the guess is too high, too low, or correct.
- Chances: The player has 5 attempts to get it right.
Why This Game is Great for Kids
- Teaches loops and conditions (for, if, else).
- Introduces random numbers.
- Fun and interactive way to learn programming.
Next Challenge
- Try changing the number range (1 to 50, or 1 to 100).
- Add more chances.
- Let the game give a score for each correct guess.
Want more fun coding projects for kids?
Visit LittleTechLearners.com and follow us on Instagram @LittleTechLearners_ for exciting coding tips, activities, and tutorials.