Introduction
The guessing game is a classic first project because it combines a loop, input validation, conditions and a little randomness. This version adds warmth hints, ignores repeated guesses and keeps a best score across rounds.
Problem statement
Objectives
- Write a game loop with while and break
- Validate user input without crashing
- Use random.Random with a seed for repeatable behaviour
- Track state across rounds
Features
- Higher/lower hints with very hot / warm / cold clues
- Seven attempts; invalid and repeated guesses don't count
- Play-again loop with wins and best score
- Optional seed for repeatable games
- Clean exit when input ends
Concepts demonstrated
- Game loops
- Input validation
- Seeded random numbers
- Functions returning optional values
System architecture
ask wraps input() and echoes the line when input comes from a file, so test transcripts are readable. play_round runs one game and returns the attempts used on a win or None. hint builds the higher/lower message with a warmth clue based on the distance. main reads the seed, creates a random.Random and loops over rounds while tracking wins and the best score.
Folder structure
Complete source code
main.py
What each file does
- main.py
- The whole program: data classes, validation, the command loop and the report.
How to run it
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
7 50 abc 150 25 37 45 42 y 50 30 10 15 15 25 18 19 n
Testing
- Run
python main.py < tests/sample-input.txtand compare withtests/expected-output.txt. - Seed 7 makes the secrets 42 and then 20.
- In game 1, 'abc' and 150 are rejected without costing an attempt, and 42 is found on the fifth attempt.
- In game 2 the repeated guess 15 doesn't count, and after seven attempts the number 20 is revealed.
Common errors
ValueError when the player types a word
Check answer.isdigit() before calling int(), and ask again.
The same numbers appear every game
Only pass a seed for testing; random.Random() without a seed uses fresh randomness.
Possible improvements
- Add difficulty levels with different ranges and attempt limits
- Save the best score to a file between runs
- Show the optimal number of guesses (binary search) after each round
Additional challenges
- 1Let the computer guess your number using binary search.
- 2Add a two-player mode where one player sets the number.
- 3Give a score that depends on attempts and time taken.
Built and tested it yourself? Mark it to update your dashboard.