Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions guessing-game/README.md
Original file line number Diff line number Diff line change
@@ -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.
39 changes: 39 additions & 0 deletions guessing-game/guessing-game.py
Original file line number Diff line number Diff line change
@@ -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.")