A simple yet polished Number Guessing Game written in Python.
This game demonstrates clean coding practices, Pythonic refactoring, type hinting, and structured documentation using PEP 8 and PEP 257 standards.
The player tries to guess a randomly chosen number within a user-defined range.
The program provides hints, tracks the number of attempts, and offers an optional โcheat modeโ for debugging or fun testing.
This project was refactored to:
- Replace manual loops with Python built-ins (
next(),join(),enumerate()). - Add comprehensive docstrings for all functions.
- Include type hints for clarity and IDE/static analysis support.
- Follow PEP 8 (style) and PEP 257 (docstring) guidelines.
- Improve game structure and flow for readability and reusability.
โ
User-defined range for random number
โ
Cheat input (-1) reveals the secret number
โ
Hint system for higher/lower and โwithin 1โ feedback
โ
Multiple attempts (default 4)
โ
Clean game summary for each round
โ
Play again option
โ
Fully type-annotated, well-documented code
-
Clone the repository:
git clone https://github.com/husseini2000/Number-Gussing-Game.git cd number-guess-game -
Run the game:
python number_guess.py
- Youโll be asked to enter a lowest and highest number.
- The computer randomly selects a number between them.
- Youโll have 4 attempts to guess it.
- If your guess is close (within 1), youโll get a hint.
- Enter -1 to reveal the answer.
- After the round, you can choose to play again.
number_guess.py
โโโ wel_message() # Display welcome banner
โโโ number_to_guess() # Set range and generate random number
โโโ get_guess() # Collect user guesses safely
โโโ handle_cheat_input() # Reveal number if user enters -1
โโโ give_feedback() # Provide intelligent feedback
โโโ play_round() # Control game logic per round
โโโ play_game() # Run overall game loop
| Issue | Description |
|---|---|
| ๐ Repetitive loops | Used while loops for checks that can be replaced by built-ins (next, join, any). |
| โ๏ธ Hardcoded retry logic | Maximum attempts (4) were fixed without easy modification. |
| ๐ฏ Random number persistence | The same random number was reused across new rounds. |
| ๐งพ No type hints or docstrings | Functions lacked clear definitions and expected input/output documentation. |
| ๐งฉ Confusing cheat logic | The โcheat modeโ function name didnโt reflect its simple purpose. |
| ๐จ Weak error handling | Non-integer input could crash the program. |
| ๐งน Unclear flow | Replay and attempt tracking were spread across multiple functions, reducing readability. |
| Category | Improvement |
|---|---|
| ๐ง Readability | All functions now have PEP 257โcompliant docstrings and clear names. |
| ๐ Maintainability | Type hints added to every function and variable where applicable. |
| ๐งฐ Pythonic Style | Replaced manual loops with next(), join(), and comprehensions. |
| ๐งฉ Error Handling | Input validation ensures non-integer inputs are handled gracefully. |
| ๐ฎ User Experience | Added feedback messages, summary of guesses, and emojis for engagement. |
| ๐งฑ Extensibility | Structure supports future features (e.g., difficulty levels, scoring). |
- Language: Python 3.10+
- Libraries: Built-in (
random,typing) - Standards: PEP 8 (style), PEP 257 (docstrings), Type Hints (PEP 484)
- ๐งฉ Difficulty Levels: Easy / Medium / Hard (dynamic range and attempts)
- ๐ Score Tracking: Maintain win/loss stats across sessions
- ๐พ Logging: Replace prints with Pythonโs
loggingfor better debugging - ๐ Unit Testing: Add
pytesttests for each function - ๐ป CLI / GUI Version: Expand into a command-line or graphical version using Tkinter or PySimpleGUI
Al-Husseini Ahmed Abdelaleem
This project is open-source and available under the MIT License.
This project demonstrates how a simple Python program can evolve into a clean, professional, and maintainable codebase through refactoring and adherence to best practices.
โClean code always looks like it was written by someone who cares.โ โ Robert C. Martin