My First Python Game: "Guess the Number"

As a beginner in Python, I wanted to challenge myself with a fun and practical project. So I decided to create a Number Guessing Game — a simple text-based game where the computer picks a random number, and the player has to guess it.
This project helped me understand how to:
Take input from the user
Use conditional logic
Handle errors
Work with loops
Use the
random
module in Python
How the Game Works
Here’s the basic idea:
The program randomly picks a number between 1 and 10.
The user has up to 5 chances to guess the number.
After 5 guesses, the game asks if the user wants to continue.
If the user types yes, they get 5 more guesses.
If the user guesses correctly at any time, they win that round.
If the user types something that isn't a number, it shows a friendly error message.
What I Learned
While working on this project, I practiced:
Using
random.randint()
to generate a random number.try
andexcept
blocks to handle invalid input (like typing a word instead of a number).while True
loops to keep the game running.Resetting variables and controlling program flow with
if
,else
, andbreak
.
🧾 Full Python Code
pythonCopyEditimport random
random_number = random.randint(1,10)
counts = 0
guess_limit = 5
while True:
try:
user_guess = int(input("Guess a Number: "))
counts += 1
if user_guess == random_number:
print(f"Correct you got it in {counts} rounds: ")
counts = 0
elif user_guess > random_number:
print("Too high!")
else:
print("Too low!")
if counts == guess_limit:
choice = input("You are out of guesses! Do you want to continue (yes/no)? ").strip().lower()
if choice == "yes":
counts = 0
random_number = random.randint(1,10)
print("You have 5 More Guesses")
else:
print("Thank you for playing!")
break
except ValueError:
print("Enter a valid number:")
Challenges I Faced
Some parts that took me a little time to figure out:
Making sure the game doesn’t crash if someone types text instead of a number.
Resetting the guess count properly after 5 attempts.
Keeping the game flexible and user-friendly.
What’s Next?
Here are a few things I’m thinking about adding next:
Let the player choose the difficulty (like guessing between 1–100).
Add a scoreboard that tracks how many rounds the player wins.
Final Thoughts
This project may be small, but it taught me a lot. If you're just starting out with Python, I highly recommend trying it. It's simple, interactive, and a great way to learn the basics of programming.
Subscribe to my newsletter
Read articles from kasumbi phil directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
