Introduction:
- As part of my learning in Python and problem-solving, I built a Number Guessing Game and extended it with a Leaderboard system.
- This project started as a simple guessing game but later evolved into a small system that tracks and sorts player performance.
🎮 How the Game Works:
The idea is simple:
- The program generates a random number
- The player tries to guess it
- After each guess:
- If the guess is too low → show hint
- If too high → show hint
- The game continues until the correct number is guessed
- The number of attempts is recorded CODE:
import random
def play_game():
number = random.randint(1, 100)
attempts = 0
while True:
guess = int(input("Enter your guess: "))
attempts += 1
if guess < number:
print("Too low!")
elif guess > number:
print("Too high!")
else:
print(f"Correct! You guessed it in {attempts} attempts.")
return attempts
⚙️ Adding Difficulty Levels:
To make the game more interesting, I introduced difficulty levels:
- Easy → 1 to 50
- Medium → 1 to 100
- Hard → 1 to 200 This changes how challenging the game feels.
🏆 Leaderboard System:
Instead of stopping at just gameplay, I added a Leaderboard to store results.
Each record contains:
- Player name
- Difficulty level
- Number of attempts
leaderboard = [
{"name": "Ashiq", "difficulty": "Easy", "attempts": 3},
{"name": "John", "difficulty": "Hard", "attempts": 7}
]
🔄 Sorting the Leaderboard:
To analyze performance, I implemented sorting based on:
Attempts (fewer attempts = better)
Difficulty level
def sort_leaderboard(data, key, reverse=False):
return sorted(data, key=lambda x: x[key], reverse=reverse)
🔍 How the System Works:
This project combines two main parts:
- Game Logic Handles:
- Random number generation
- User input
- Hint system
Attempt tracking
Data Handling
Handles:Storing player results
Sorting leaderboard
Displaying rankings
⏱️ Complexity:
- Game loop → depends on user attempts
- Sorting → O(n log n)
⚠️ Challenges Faced:
- Handling invalid user input
- Designing a clean leaderboard structure
- Implementing flexible sorting options
Top comments (0)