Python Series track

Project 2 · Practice Projects

Project 2 — Number Guessing Game

Guess the secret number with too-high/too-low hints. Add difficulty levels (attempts limit) and a high-score leaderboard.

Practice Projects1 min readFree lesson

Difficulty: Lv ★ | Prerequisite Knowledge: Basic Weeks 4-5 + Week 9 (Functions)

Goal

Guess a random number between 1~N — attempt limit + difficulty selection

Differences from Basic Assignment

  • Basic Week 5 = simple addition Large/Small + number of attempts
  • Here = attempt limit + difficulty selection + retry option + function separation

Requirements

  • Select difficulty at the start (Easy 150/7-times, Normal 1100/7-times, Hard 1~500/10-times)
  • Hint + remaining attempts for each try
  • Reveal answer if attempts exceeded

Input/Output Example

Difficulty (1. Easy 2. Normal 3. Hard): 2
Guess a number from 1 to 100 within 7 tries

[Remaining 7 tries] Number: 50 → Higher number
[Remaining 6 tries] Number: 75 → Lower number
[Remaining 5 tries] Number: 63 → Correct! (3 tries)

One more time? (y/n): n

Challenge Task

  • Save best record file
  • User sets the answer and computer tries to guess

Execution

python solution.py

Grading Checklist

  • Difficulty selection
  • Attempt limit
  • Separate into 3 or more functions

Assignment

Show the reference solution

Try the assignment yourself first — comparing your attempt with a working solution is where most of the learning happens.

solution.pyPython
"""Number Guessing — Choose Difficulty + Attempt Limit"""
import random

LEVELS = {
    "1": ("Easy", 1, 50, 7),
    "2": ("Medium", 1, 100, 7),
    "3": ("Hard", 1, 500, 10),
}

def pick_difficulty():
    while True:
        choice = input("Difficulty (1. Easy 2. Medium 3. Hard):").strip()
        if choice in LEVELS:
            return LEVELS[choice]
        print("Choose from 1, 2, or 3")

def play_round(low, high, max_tries):
    answer = random.randint(low, high)
    print(f"Try to guess within {low}~{high}, {max_tries} times")
    for left in range(max_tries, 0, -1):
        try:
            guess = int(input(f"[Remaining {left} tries] Number:"))
        except ValueError:
            print("Enter numbers only")
            continue
        if guess < answer:
            print("→ Higher number")
        elif guess > answer:
            print("→ Lower number")
        else:
            print(f"✓ Correct! (Attempt {max_tries - left + 1})")
            return True
    print(f"Attempts exceeded! The answer is {answer}")
    return False

def main():
    while True:
        name, low, high, tries = pick_difficulty()
        print(f"\n[{name}]")
        play_round(low, high, tries)
        if input("One more time? (y/n):").strip().lower() != "y":
            break

if __name__ == "__main__":
    main()

Learn this lesson with a mentor

Work through the Python Series track on your own, or join a guided cohort at our Pitampura campus and online.

Call NowEnroll Now