If you’re building your very first Python project, the number guessing game in Python is the perfect place to start in 2026 — and there’s a reason every programmer remembers making one. It’s small enough to finish in a single sitting, but it quietly teaches you the building blocks behind almost every program: loops, conditions, user input, and the random module.
In this guide you’ll get two complete, working versions with free source code — a simple one to learn from, and an upgraded one with difficulty levels, limited attempts, a scoring system, and a play-again loop. Both are tested and ready to run. This is project #1 of our 145+ Python Projects roadmap.
What This Project Does
The computer secretly picks a random number, and the player keeps guessing until they find it. After every guess, the program gives a hint — “too high” or “too low” — until the player wins. Simple to describe, surprisingly satisfying to play.
- Random secret number between 1 and 100
- “Higher / lower” hints after every guess
- Attempt counter so you can compete with friends
- Input validation (typing “abc” won’t crash it)
Version 1 — Simple Number Guessing Game (Best for Learning)
Save this as guessing_game.py and run it. Around 25 lines, no libraries to install — the random module ships with Python.
import random
secret = random.randint(1, 100)
attempts = 0
print("I'm thinking of a number between 1 and 100.")
print("Can you guess it?\n")
while True:
guess = input("Your guess: ")
if not guess.isdigit():
print("Please enter a valid number.\n")
continue
guess = int(guess)
attempts += 1
if guess < secret:
print("Too low — try a HIGHER number.\n")
elif guess > secret:
print("Too high — try a LOWER number.\n")
else:
print(f"Correct! You got it in {attempts} attempts. 🎉")
break
How the Code Works (Line by Line)
- random.randint(1, 100) — picks the secret number once, before the game starts. Both ends are included.
- while True: — an infinite loop that keeps asking until we break out of it on a correct guess. This “game loop” pattern appears in every game you’ll ever write.
- guess.isdigit() — input() always returns text, so we check it’s really a number before converting with int(). Skip this and one stray letter crashes the program.
- if / elif / else — the entire game logic in three branches: too low, too high, or correct.
- attempts += 1 — a counter that only increases on valid guesses, so typos don’t count against the player.
Version 2 — With Difficulty Levels, Scoring & Replay
Once the simple version makes sense, this upgrade turns it into a real game: three difficulty levels, limited attempts, a score based on how fast you win, and a play-again loop. It also introduces functions — your first step toward organized code.
import random
def choose_level():
print("Choose difficulty:")
print("1. Easy (1-50, 10 attempts)")
print("2. Medium (1-100, 7 attempts)")
print("3. Hard (1-200, 5 attempts)")
while True:
choice = input("Enter 1, 2 or 3: ").strip()
if choice == "1":
return 50, 10
elif choice == "2":
return 100, 7
elif choice == "3":
return 200, 5
print("Invalid choice, try again.")
def play():
top, max_attempts = choose_level()
secret = random.randint(1, top)
attempts = 0
print(f"\nI'm thinking of a number between 1 and {top}.")
print(f"You have {max_attempts} attempts. Good luck!\n")
while attempts < max_attempts:
guess = input(f"Attempt {attempts + 1}/{max_attempts} — your guess: ")
if not guess.isdigit():
print("Please enter a valid number.\n")
continue
guess = int(guess)
attempts += 1
if guess < secret:
print("Too low — go HIGHER.\n")
elif guess > secret:
print("Too high — go LOWER.\n")
else:
score = (max_attempts - attempts + 1) * 10
print(f"Correct! You won in {attempts} attempts.")
print(f"Your score: {score} points 🎉\n")
return
print(f"Out of attempts! The number was {secret}. 😢\n")
if __name__ == "__main__":
while True:
play()
again = input("Play again? (y/n): ").strip().lower()
if again != "y":
print("Thanks for playing!")
break
What You’ll Learn From This Project
- The random module — generating unpredictable values, the heart of most games
- while loops & break — the game-loop pattern used everywhere from Snake to real applications
- Conditionals — clean if / elif / else decision-making
- Input validation — defensive coding that separates beginners from careful programmers
- Functions (version 2) — structuring code into reusable pieces
5 Ways to Extend This Project
Finished both versions? Make it yours — that’s where the real learning is:
- High score file — save the best score to a text file so it survives between games
- Two-player mode — one player sets the number, the other guesses
- Reverse mode — YOU think of a number and the computer guesses it (teaches binary search!)
- Hot & cold hints — “very close!” when within 5, “freezing” when far away
- GUI version — rebuild it with Tkinter buttons and a text box
When you’re ready for the next challenge, try the Simple Calculator in Python — project #2 on the roadmap.
Project #1 done — 144 to go 🚀
Head back to the full roadmap and pick your next build.
Frequently Asked Questions
It’s arguably the best first project. It covers loops, conditions, input, and randomness — the four things nearly every Python program uses — in under 30 lines of code.
No. The random module is built into Python, so any Python 3 installation runs this project immediately.
Binary search: always guess the middle of the remaining range. For 1–100, start at 50, then 25 or 75, and so on. You’ll find any number in at most 7 guesses — and that strategy is a genuine computer-science concept.
You’re probably calling int(input()) directly. Use the isdigit() check from our code first — it validates the input before converting, so letters just show a friendly message instead of crashing.
Yes, it’s completely free. Read the line-by-line section so you can explain it, add one extension of your own, and you’ll genuinely understand what you’re submitting.



