Guess The Number Game Documentation
Guess The Number Game Documentation
Submitted By:
Mamoona Nadeem
SUPERVISOR
Najam ul Sehar
Lecturer
School of Information Technology
1. Introduction
The Guess the Number game is a classic and engaging Python program designed to help users
practice their logical thinking and problem-solving skills. The game is simple yet entertaining,
making it an excellent project for beginners learning Python programming.
Objective
The primary objective of the game is to guess a randomly generated number within a specified
range (default is 1 to 100). The player is provided with hints after each guess, indicating whether
their guess was too high or too low. The game continues until the player correctly guesses the
number.
Target Audience
This game is ideal for:
Core Features
1. Random Number Generation:
o The game generates a random number between 1 and 100 using Python's random
module.
2. User Input Validation:
o The program ensures that the player enters a valid integer. If the input is invalid
(e.g., text or symbols), the game prompts the player to try again.
3. Feedback System:
o After each guess, the game provides feedback such as "Too high" or "Too low" to
guide the player toward the correct number.
4. Attempt Counter:
o The game keeps track of the number of attempts the player makes to guess the
correct number.
5. Simple and Interactive:
o The game is easy to play and provides a fun, interactive experience for users of all
ages.
Additional Features
• Customizable Number Range: Players can modify the range of numbers to make the
game easier or harder.
• Difficulty Levels: The game can be expanded to include different difficulty levels (e.g.,
easy, medium, hard).
• Score Tracking: A scoring system can be added to reward players for guessing the
number in fewer attempts.
3. How It Works
The game follows a straightforward workflow to ensure a smooth and enjoyable experience for
the player. Here’s a step-by-step explanation of how it works:
Step-by-Step Workflow
1. Random Number Generation:
o The program uses the random.randint() function to generate a random number
between 1 and 100. This number is stored as the target number that the player
needs to guess.
2. User Input:
o The player is prompted to enter their guess. The program checks if the input is a
valid integer. If not, it displays an error message and asks for input again.
3. Comparison and Feedback:
o The program compares the player's guess with the target number. o If the
guess is too low, the program informs the player to guess higher. o If the guess is
too high, the program informs the player to guess lower. o If the guess is
correct, the game ends, and the program displays a congratulatory message along
with the number of attempts.
4. Loop Until Correct Guess:
o The game continues to prompt the player for guesses until they correctly guess the
number.
5. End of Game:
o Once the player guesses the correct number, the program displays the total
number of attempts and exits.
Flowchart (Optional)
A flowchart can be included to visually represent the game's workflow. For example:
Copy
Start → Generate Random Number → Prompt User Input → Validate Input → Compare Guess → Provide
Feedbac k → Repeat Until Correct Guess → Display Result → End
4. Code Structure
The code is structured into a single main function, guess_number(), which contains all the game
logic. Here’s a breakdown of the code structure:
Modules Used
• random: This module is used to generate a random number.
• input Handling: The program uses Python's built-in input() function to accept user input.
Functions
• guess_number():
o This is the main function that contains the game logic. o It initializes the
random number, handles user input, and provides feedback. Variables
• number_to_guess: Stores the randomly generated target number.
• attempts: Tracks the number of guesses made by the player.
• guessed_correctly: A boolean variable that controls the game loop. It is set to True when
the player guesses the correct number.
Error Handling
The program uses a try-except block to handle invalid inputs (e.g., non-integer values). If
the player enters an invalid input, the program displays an error message and prompts the
player to try again.
Requirements
• Python 3.x: Ensure that Python 3.x is installed on your system. You can download it
from the official Python website: python.org.
• No Additional Libraries: The game uses only Python's built-in modules, so no
additional libraries are required.
Steps to Run
1. Download or copy the code from the provided source.
2. Save the code as a .py file (e.g., guess_number.py).
3. Open a terminal or command prompt and navigate to the directory where the file is saved.
4. Run the script using the following command:
Copy
python guess_number.py
5. The game will start, and you can begin playing immediately.
6. Usage Instructions
The Guess the Number game is simple and intuitive to play. Here’s how you can use it: How
to Play
Rules
• The number to guess is always between 1 and 100 (unless you modify the range).
• Invalid inputs (e.g., text or symbols) will prompt the game to ask for a valid number.
There is no limit to the number of attempts, so you can keep guessing until you get it
right.
7. Example Output
Here’s an example of how the game works:
Copy
Welcome to the Guess the Number game!
I'm thinking of a number between 1 and
100. Enter your guess: 50 Too high! Try
again. Enter your guess: 25 Too low! Try
again.
Enter your guess: 37
Too high! Try again.
Enter your guess: 31
Congratulations! You've guessed the number in 4 attempts.
8. Customization Options
The Guess the Number game is highly customizable. Here are some ways you can modify it to
suit your preferences:
Copy
Limit Attempts
Add a maximum number of attempts (e.g., 10 guesses). If the player exceeds this limit,
the game ends.
Score System
Assign points based on the number of attempts. For example:
o 1–5 attempts: 10 points o
6–10 attempts: 5 points o
More than 10 attempts: 1
point
9. Troubleshooting
While the game is designed to be simple and error-free, you may encounter some issues. Here’s
how to troubleshoot them:
Common Issues
1. Invalid Input:
o If the player enters a non-integer value (e.g., text or symbols), the program will
display an error message and prompt the player to try again.
2. Program Crashes:
o If the program crashes unexpectedly, ensure that all inputs are handled properly
using try-except blocks.
Solutions
Use try-except blocks to handle errors gracefully. For example:
python
Copy
try:
guess = int(input("Enter your guess: "))
except ValueError: print("Invalid input. Please
enter a valid number.")
Multiplayer Mode
Allow two players to compete against each other. The player who guesses the number in
fewer attempts wins.
Sound Effects
• Add audio feedback for correct or incorrect guesses to enhance the gaming experience.
Leaderboard
Track high scores across multiple games and display a leaderboard.
11. Conclusion
The Guess the Number game is a simple yet engaging project that demonstrates the use of basic
Python programming concepts. It is an excellent way for beginners to practice coding and
logicbuilding skills. The game is highly customizable, allowing you to modify and expand it to
suit your preferences. Whether you're a beginner or an experienced programmer, this project
offers a fun and interactive way to learn and experiment with Python.
Appendix
Full Code Listing
python
Copy
import random
def guess_number():
# Generate a random number between 1 and 100
number_to_guess = random.randint(1, 100)
attempts = 0
guessed_correctly = False
if __name__ == "__main__":
guess_number()
References
• Python Documentation: https://docs.python.org/3/
• random Module Documentation: https://docs.python.org/3/library/random.html