A simple command-line number guessing game built with Python.
The computer randomly selects a number between 0 and 100, and the player tries to guess it. After each valid guess, the game provides a hint indicating whether the guess is higher or lower than the secret number.
The project also includes automated tests using pytest.
- π² Random secret number between
0and100 - π’ Validation of user input
- π« Handling invalid and out-of-range inputs
- π‘ Higher/lower hints after each guess
- π Performance evaluation based on the number of attempts
- π Option to play multiple games
- πͺ Ability to quit the game with
q - π§ͺ Automated tests with
pytest - π§© Integration tests using
monkeypatch - π¦ Python code organized into reusable functions
- Python 3
- pytest
- Git
NUMBER_GUESSER/
β
βββ main.py # Main game logic
βββ test_game.py # Automated tests
βββ .gitignore # Ignored files and directories
βββ README.md # Project documentation
git clone <YOUR_REPOSITORY_URL>
cd NUMBER_GUESSERpython -m venv .venvActivate it on Linux/macOS:
source .venv/bin/activateActivate it on Windows:
.venv\Scripts\activateInstall pytest:
pip install pytestRun the following command:
python main.pyYou will be asked to enter a number between 0 and 100.
Example:
Please enter your number guess between 0 and 100, or press 'q' to exit: 30
Attempt #1
That is your guess: 30.
Your guess is lower than the secret number.
Please enter your number guess between 0 and 100, or press 'q' to exit: 70
Attempt #2
That is your guess: 70.
Your guess is higher than the secret number.
Please enter your number guess between 0 and 100, or press 'q' to exit: 50
Attempt #3
That is your guess: 50.
You won!
You found the number in 3 attempts!
Excellent! π―
Run the complete test suite with:
pytestOr:
python -m pytestThe test suite covers:
- Valid user input
- Invalid input
- Out-of-range values
- Boundary values
- Higher/lower/equal guesses
- Performance evaluation
- Multiple guesses during a game
- Invalid inputs during gameplay
- Quitting the game
This project was built as a practical Python learning project and focuses on several important concepts:
- Functions
- Type hints
- Exception handling with
try/except - Loops
- Conditional statements
- User input
- Random number generation
- Function decomposition
- Automated testing with pytest
pytest.mark.parametrizecapsysmonkeypatch- Basic Git workflow
- Project documentation
Possible improvements for future versions:
- Add difficulty levels
- Add a maximum number of attempts
- Add a score system
- Store high scores
- Add a graphical interface
- Improve the game architecture as the project grows
Saeed
This project is part of my journey to improve my Python programming and software development skills.