How I Built a Level-Based Number Guessing Game in Python as a Beginner (with Code & Logic Explained)


How I Built a Level-Based Number Guessing Game in Python as a Beginner
Ever get bored while learning to code? That’s exactly where I was — stuck, unmotivated, and unsure of what to build next. Then I thought… why not make a game? Something simple, fun, and logic-based.
That’s how I ended up building this Level-Based Number Guessing Game using Python.
What is This Game?
Imagine this: you're dropped into a mysterious number dimension where your only way out is to guess the right number. Sounds intense? Don’t worry — it’s actually pretty fun.
Pick your challenge: Choose from three difficulty levels — Easy, Normal, or Hard.
The computer thinks of a number: Based on your chosen level, it secretly picks a number.
You get 3 chances to hit the bullseye.
Too high? Too low? It’ll give you hints so you're not completely lost.
Play again or quit — it’s your call after each round.
It’s simple, addictive, and the perfect way to flex your logical muscles — all from your terminal!
Why I Chose This Project
This little project wasn’t just fun — it packed in a lot of hands-on learning too. Here’s what I picked up along the way:
Got comfy using randint() from the random module — no more boring static values!
Learned how to keep the game running smoothly using while loops and nested logic.
Figured out how to catch user mistakes with try-except — no more crashes when someone types “ten” instead of 10.
Built a replay loop so the game doesn’t just end… unless the player says so.
Started thinking like a developer by making the code cleaner and more reusable.
It might seem like a basic game, but it taught me a ton of essential Python skills without feeling like a boring tutorial.
How It Works (Code Walkthrough)
Here's the full code I wrote for the game:
from random import randint
print('Let\'s see how good you are at a guessing game')
# Generating random integers for each level
a = randint(1, 50)
b = randint(1, 100)
c = randint(1, 200)
d = 3 # Number of chances
def levels_useripc():
play = True
while play:
# Asking user at what level they want to play this game
while True:
try:
level = input('\nOn what level you want to play this game (Easy "1-50", Normal "1-100", Hard "1-200") -- ').capitalize()
if level not in ['Easy', 'Normal', 'Hard']:
raise ValueError('\nType a correct spelling of level\'s')
break
except ValueError as e:
print(e)
# Set the target number based on level
if level == 'Easy':
target = a
elif level == 'Normal':
target = b
elif level == 'Hard':
target = c
chances = d
while chances > 0:
try:
user_ip = int(input('Type a number -- '))
except ValueError:
print('\nType a number, not an alphabet or word!!')
continue
if user_ip == target:
print('\nYou are a Master in this Game')
break
elif user_ip < target:
print('You went too low')
else:
print('You went too high')
chances -= 1
if chances > 0:
print(f'You have {chances} chances left.')
else:
print('\nNo more chances left.')
again = input("\nDo you want to play again? (yes/no): ").lower()
if again not in ['yes', 'y']:
play = False
print("Thanks for playing!")
if __name__ == '__main__':
levels_useripc()
That’s It… I Guess?
Is this the next big thing in gaming? Nope. But hey — it doesn’t crash, and it let me flex my Python muscles a bit.
If you try this and guess the number on your first try, I owe you a high five. Digitally, of course.
Peace out, and keep coding.
Subscribe to my newsletter
Read articles from Kaustubh Rane directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
