This is a simple Python number guessing game where the program randomly selects a number between 1 and 20. The player has five chances to guess the correct number.
If the guess matches the random number, the player wins; otherwise, the program prompts the user to try again until all guesses are used.
- Random number generation between 1 and 20
- Five attempts per game
- Input validation using the
pyinputpluslibrary - Fun, lightweight console gameplay
Make sure you have Python 3.x installed, along with the following library:
pip install pyinputplus-
Save the script as
number_guess.py. -
Open a terminal or command prompt in the script directory.
-
Run the program:
python number_guess.py
-
Follow the prompt to guess the number (you get 5 tries).
-
The program will reveal the correct number when the game ends.
I'm thinking of a number between 1 and 20. You have 5 guesses.
Guess the number. 10
Try again.
Guess the number. 15
You guessed correctly!
The correct answer was 15. Game over...
- You can customize the number range by changing the values in
random.randint(1, 20). - Adjust the number of guesses by modifying the range in
for i in range(5).