-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #234 from pratheekv39/numberguessinggame
#201 Added Number Guessing Game
- Loading branch information
Showing
2 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Number Guessing Game 🎲 | ||
|
||
This is a simple **Python-based command-line** implementation of the classic **Number Guessing Game**. In this game, you need to guess a randomly generated number within a specified range. The game provides feedback after each guess to help you find the correct number! | ||
|
||
--- | ||
|
||
## 🎯 Objective | ||
|
||
The goal is to guess the secret number generated by the computer within the range of 1 to 100. After each guess, you'll receive feedback indicating whether your guess was too high, too low, or correct. | ||
|
||
--- | ||
|
||
## 🚀 How to Play | ||
|
||
1. **Start the Game**: Run the Python script to start the game. | ||
2. **Make a Guess**: Enter a number between 1 and 100 when prompted. | ||
3. **Receive Feedback**: After each guess, you'll get one of three responses: | ||
- "Too low! Try again." if your guess is lower than the secret number. | ||
- "Too high! Try again." if your guess is higher than the secret number. | ||
- "Congratulations! You guessed the number in X attempts!" if you guess correctly. | ||
4. **Keep Guessing**: Continue guessing until you find the correct number. | ||
5. **Play Again**: After each game, you'll be asked if you want to play again. | ||
|
||
--- | ||
|
||
## 🛠 System Requirements | ||
|
||
- **Operating System**: Any system running Python 3.x | ||
- **Python Version**: Python 3.x or higher | ||
|
||
### Dependencies | ||
|
||
This game uses only Python's built-in libraries, so no additional dependencies are required. | ||
|
||
--- | ||
|
||
## 🔧 How to Run | ||
|
||
1. Clone the repository and navigate to the project folder: | ||
```bash | ||
git clone <repository-url> | ||
cd <project-folder> | ||
``` | ||
|
||
2. Run the Python script: | ||
```bash | ||
python3 number_guessing_game.py | ||
``` | ||
|
||
3. Enjoy playing the Number Guessing Game! | ||
|
||
--- | ||
|
||
## 📚 Game Mechanics | ||
|
||
- **Generated Number**: The game will randomly generate a number between 1 and 100. | ||
- **User Input**: You will enter your guess as an integer. | ||
- **Feedback**: | ||
- "Too low!" if your guess is lower than the secret number. | ||
- "Too high!" if your guess is higher than the secret number. | ||
- "Congratulations!" when you guess correctly. | ||
- **Attempt Counter**: The game keeps track of how many attempts you've made. | ||
- **Play Again**: After each game, you can choose to play again or exit. | ||
|
||
--- | ||
|
||
## 💻 System Specifications | ||
|
||
- Python Version: 3.x+ | ||
- OS: Any system capable of running Python 3.x | ||
|
||
--- | ||
|
||
## 📖 Additional Information | ||
|
||
The Number Guessing Game is a great way to practice your intuition and deduction skills. It's also an excellent introduction to basic programming concepts like loops, conditionals, and random number generation. | ||
|
||
--- | ||
|
||
## 🤔 Strategy Tips | ||
|
||
1. Start with a guess in the middle of the range (e.g., 50). | ||
2. Based on the feedback, eliminate half of the remaining possibilities. | ||
3. Continue narrowing down the range until you find the correct number. | ||
4. Try to guess the number in as few attempts as possible! | ||
|
||
--- | ||
|
||
Enjoy playing the Number Guessing Game! 🎉 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import random | ||
|
||
def play_game(): | ||
# Generate a random number between 1 and 100 | ||
secret_number = random.randint(1, 100) | ||
attempts = 0 | ||
|
||
print("Welcome to the Number Guessing Game!") | ||
print("I'm thinking of a number between 1 and 100.") | ||
|
||
while True: | ||
# Get the player's guess | ||
try: | ||
guess = int(input("Enter your guess: ")) | ||
except ValueError: | ||
print("Please enter a valid number.") | ||
continue | ||
|
||
attempts += 1 | ||
|
||
# Check the guess and provide feedback | ||
if guess < secret_number: | ||
print("Too low! Try again.") | ||
elif guess > secret_number: | ||
print("Too high! Try again.") | ||
else: | ||
print(f"Congratulations! You guessed the number in {attempts} attempts!") | ||
break | ||
|
||
def main(): | ||
while True: | ||
play_game() | ||
|
||
# Ask if the player wants to play again | ||
play_again = input("Do you want to play again? (yes/no): ").lower() | ||
if play_again != 'yes': | ||
print("Thanks for playing! Goodbye!") | ||
break | ||
|
||
if __name__ == "__main__": | ||
main() |