From 793cb5049186ecc766828a22f2890d538c68dadf Mon Sep 17 00:00:00 2001 From: Victor Rodrigues Date: Thu, 24 Jul 2025 08:26:04 -0300 Subject: [PATCH] feat: add guessing game project with README and instructions --- guessing-game/README.md | 80 ++++++++++++++++++++++++++++++++++ guessing-game/guessing-game.py | 39 +++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 guessing-game/README.md create mode 100644 guessing-game/guessing-game.py diff --git a/guessing-game/README.md b/guessing-game/README.md new file mode 100644 index 0000000..c472aa7 --- /dev/null +++ b/guessing-game/README.md @@ -0,0 +1,80 @@ +# 🎯 Guessing Game (Beginner Python Project) + +Welcome to the **Guessing Game**, a beginner-friendly Python project where the player tries to guess a random number between 1 and 100 in 7 attempts. + +This is a great practice for: +- Working with loops and conditionals +- Using random number generation +- Handling user input and exceptions + +--- + +## 📌 How It Works + +- The computer selects a random number from 1 to 100. +- You have 7 tries to guess the correct number. +- After each guess, you'll be told whether your guess is too high or too low. +- If you guess the number, you win. If you use all your attempts, the game ends. + +--- + +## ▶️ Getting Started + +### ✅ Requirements + +- Python 3.x installed on your machine + You can check with: + +```bash +python --version +``` + +## 🛠️ How to Run the Game +1. Clone this repository (or fork if you haven't yet): +```bash +git clone https://github.com/your-username/Python_Project.git +cd Python_Project/guessing-game +``` +2. Run the script: +```bash +python guessing-game.py +``` + +You’ll play the game directly in the terminal. + +## 🧠 Concepts Covered +`random.randint()` + +`input()` and type casting + +`try/except` for error handling + +`while` loop and basic logic flow + +## 🧩 Example Output + +```bash +Welcome to the Guessing Game! +I'm thinking of a number between 1 and 100. +You have 7 attempts to guess it correctly. + +Enter your guess: 50 +Too low. Try a higher number. +Attempts left: 6 + +Enter your guess: 75 +Too high. Try a lower number. +Attempts left: 5 +... +🎉 Congratulations! You guessed the number! +``` + + + +## 🤝 Contributions +This game was added by @VictorMelkor as part of a learning initiative to help beginners get started with Python scripting and open source. + +Feel free to improve it or suggest enhancements! + +## 📄 License +This project is licensed under the MIT License. \ No newline at end of file diff --git a/guessing-game/guessing-game.py b/guessing-game/guessing-game.py new file mode 100644 index 0000000..5ee3093 --- /dev/null +++ b/guessing-game/guessing-game.py @@ -0,0 +1,39 @@ +import random + +# Introduction +print("Welcome to the Guessing Game!") +print("I'm thinking of a number between 1 and 100.") +print("You have 7 attempts to guess it correctly.") + +# Generate a random number between 1 and 100 +secret_number = random.randint(1, 100) + +# Set the number of allowed attempts +attempts = 7 + +# Game loop +while attempts > 0: + try: + # Ask the user for their guess + guess = int(input("Enter your guess: ")) + + # Check if the guess is correct + if guess == secret_number: + print("🎉 Congratulations! You guessed the number!") + break + elif guess < secret_number: + print("Too low. Try a higher number.") + else: + print("Too high. Try a lower number.") + + # Decrease remaining attempts + attempts -= 1 + print(f"Attempts left: {attempts}\n") + + except ValueError: + # Handle non-integer input + print("Invalid input. Please enter a number.\n") + +# If no attempts are left +if attempts == 0: + print(f"❌ Game over! The number was {secret_number}. Better luck next time.")