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.
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
Plan the game loop
Write the flow on paper: pick difficulty โ pick secret โ ask guess โ compare โ repeat until correct โ show tries โ play again?
- 2
Pick the secret number
Use random.randint(low, high). Keep low and high in a dict per difficulty: {"easy": (1, 10), ...}.
- 3
Read guesses safely
Wrap int(input()) in try/except ValueError and loop until the player types a valid number inside the range.
- 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
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
Add a replay menu
After a win, ask "Play again? (y/n)" and loop back to the difficulty menu.
- 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