๐ŸŽ™ Free live masterclass ยท Sun, 29 Nov, 7:00 pm IST ยท Java Full Stack roadmap Register free โ†’
Skills101

Number guessing game

The computer picks a secret number and the player guesses it with "higher" or "lower" hints, with difficulty levels and a best-score record.

Beginnerโฑ 2โ€“3 hours๐Ÿชœ 7 stepsPython

What you'll learn

  • โ—†Loops, conditions and input validation
  • โ—†The random module
  • โ—†Saving a small high-score file

What you'll build

  • โœ“Easy, medium and hard ranges (1โ€“10, 1โ€“100, 1โ€“1000)
  • โœ“Higher/lower hints and a guess counter
  • โœ“Rejects non-numbers without crashing
  • โœ“Best score saved between runs

Step-by-step guide

  1. 1

    Plan the game loop

    Write the flow on paper: pick difficulty โ†’ pick secret โ†’ ask guess โ†’ compare โ†’ repeat until correct โ†’ show tries โ†’ play again?

  2. 2

    Pick the secret number

    Use random.randint(low, high). Keep low and high in a dict per difficulty: {"easy": (1, 10), ...}.

  3. 3

    Read guesses safely

    Wrap int(input()) in try/except ValueError and loop until the player types a valid number inside the range.

  4. 4

    Give hints and count tries

    Compare the guess with the secret, print "Too high" or "Too low", and increase a tries counter each round.

  5. 5

    Save the best score

    Store the fewest tries per difficulty in best.json with the json module; read it at start, update it when the player beats it.

  6. 6

    Add a replay menu

    After a win, ask "Play again? (y/n)" and loop back to the difficulty menu.

  7. 7

    Split into functions

    Refactor into choose_difficulty(), play_round(), load_scores() and save_scores() so main() reads like the plan from step 1.

๐Ÿš€ Make it stand out

  • +Limit the number of guesses on hard mode
  • +Add a two-player mode where one player sets the number
  • +Build a Tkinter window version