This is a simple Python project where the computer randomly picks a number, and the player tries to guess it in the fewest attempts possible.
- The first computer “guessing games” appeared in the 1960s, written in BASIC.
- Games like this helped teach binary search, a powerful computer science concept.
- Guessing games are often used in beginner coding courses to teach loops, logic, and debugging.
The goal of this project is to practice using:
- Loops
- Conditional statements
- User input
- The
randommodule in Python
By completing this project, you’ll:
- Practice designing loops and conditionals
- Learn to handle user input safely
- Understand how random number generation works
- Improve problem-solving and debugging skills
When the player enters a guess, the program tells them whether the guess is too high, too low, or correct. It also counts how many tries it took to get the right answer.
- Easy to understand and run
- Validates user input (no crashes if you type letters)
- Tells you whether your guess is higher or lower
- Lets you play again without restarting the program
- Adjustable number range
This project was created to:
- Practice using Python loops and conditional statements
- Learn to handle user input safely and avoid crashes
- Build confidence with logic-based games
- Explore simple debugging and testing
- Create a fun way to learn coding basics
- Start near the middle of the range to narrow your guesses quickly
- Use the “Very Close” hints to adjust efficiently
- Try Hard mode for more challenge
- If you get stuck, use the quit command to see the answer
- Experiment with more advanced computer science strategies like binary search to get better results
- Python 3.11 or newer
Check your version:
python --versionor on some Windows systems:
py --version- Download or clone this repository.
- Open the folder in Visual Studio Code or any code editor.
- Run the program in the terminal:
or
python guess.py
py guess.py
- Run the game in your terminal or IDE.
- The computer will pick a secret number between the given range.
- Enter your guesses until you find the correct number.
- The game will give you hints like “Too high!” or “Very close!”.
- You can type
qanytime to give up and see the correct answer.---
I'm thinking of a number between 1 and 100.
Your guess: 50
Too low!
Your guess: 75
Too high!
Your guess: 63
Correct! You got it in 3 tries.
guess.py– main game fileplay_once()– runs a single round of the gameread_int()– safely reads user input- Constants like
LOW,HIGH, andMAX_TRIEScontrol game difficulty - Optional color helper
c()adds colorful text output (if available)
Here are a few things to watch out for:
- ❌ Typing letters instead of numbers will cause invalid input messages
- ⚙️ Make sure your file name is
guess.pybefore running - 💡 Always press Enter after typing a guess
- 🧮 Remember: the secret number is always within your chosen range!
In the file guess.py, you can edit these two variables:
LOW, HIGH = 1, 100Change them to any range you want (for example, 1–1000).
- Add difficulty levels (Easy, Medium, Hard)
- Save and display a high score
- Show how close the guess is (e.g., "very close" or "way off")
- Limit the number of attempts
- Add sound or color feedback for fun
- The game uses Python’s built-in
randommodule to generate the secret number. - A loop keeps asking for guesses until the player wins or runs out of attempts.
- Conditional statements decide whether each guess is too high, too low, or correct.
- Extra feedback like “Very close!” is based on how far the guess is from the secret number.
If the game doesn’t run correctly:
- Make sure you have Python 3.11 or newer installed
- Check that you are running the command in the correct folder:
python guess.py
You can modify MAX_TRIES to make the game easier or harder:
- Easy Mode:
MAX_TRIES = 10 - Normal Mode:
MAX_TRIES = 7 - Hard Mode:
MAX_TRIES = 5
Try adjusting the range too:
LOW, HIGH = 1, 50 # Short range for quick games
LOW, HIGH = 1, 1000 # Long range for challenge
## How to Contribute
If you’d like to make this project better:
1. **Fork** this repository on GitHub
2. **Clone** your fork:
```bash
git clone https://github.com/<your-username>/number-guess-game.git- Create a new branch for your changes:
git checkout -b feature/your-feature-name
- Make and test your changes
- Commit and push your updates:
git commit -m "Added difficulty mode" git push origin feature/your-feature-name - Open a Pull Request describing what you changed
If you want to keep improving this project, try:
- Adding a timer to track how fast you guess the number
- Creating a scoreboard to store best results
- Letting players choose their own range or difficulty
- Turning the game into a GUI app using Tkinter
- Refactoring code into smaller functions for readability
Created by Zelin Huang for learning and practicing Python basics.