Number Guessing Game with Leaderboard
Number Guessing Game with Leaderboard
Introduction
The Number Guessing Game is a simple interactive Python program where the user tries to guess a randomly generated number. To make the game more meaningful and realistic, a leaderboard system is introduced to store and display player performance. This task focuses on retrieving leaderboard data and sorting it based on difficulty and number of attempts.
Game Overview
The game generates a random number, and the player tries to guess it within a limited number of attempts. Based on the difficulty level (Easy, Medium, Hard), the range of numbers and allowed attempts vary.
Leaderboard Concept
The leaderboard stores details such as:
Player Name
Difficulty Level
Number of Attempts
This data can be stored in a list of dictionaries (simulating a database).
leaderboard = [
{"name": "Alice", "difficulty": "Easy", "attempts": 3},
{"name": "Bob", "difficulty": "Hard", "attempts": 7},
{"name": "Charlie", "difficulty": "Medium", "attempts": 5}
]
Approach: List of dictionaries
Why: This structure is simple, flexible, and easy to iterate and sort.
1. Displaying Leaderboard Details
for player in leaderboard:
print(player)
Approach: Iteration using loop
Why: Allows us to access and display each record clearly.
Alternative: Could format output using f-strings for better readability.
2. Sorting Leaderboard by Difficulty
sorted_by_difficulty = sorted(leaderboard, key=lambda x: x["difficulty"])
print(sorted_by_difficulty)
Approach: Using sorted() with lambda
Why chosen: Lambda function allows custom sorting based on dictionary keys.
Note: Sorting is alphabetical (Easy, Hard, Medium).
3. Sorting by Attempts
sorted_by_attempts = sorted(leaderboard, key=lambda x: x["attempts"])
print(sorted_by_attempts)
Approach: Sorting based on numeric value
Why: Helps identify best performers (least attempts).
4. Sorting in Descending Order
sorted_desc = sorted(leaderboard, key=lambda x: x["attempts"], reverse=True)
print(sorted_desc)
Approach: Using reverse=True
Why: Useful when we want to see highest attempts or worst performance.
5. Combined Sorting (Difficulty + Attempts)
sorted_combined = sorted(leaderboard, key=lambda x: (x["difficulty"], x["attempts"]))
print(sorted_combined)
Approach: Tuple-based sorting
Why: First sorts by difficulty, then by attempts within each difficulty level.
Why This Approach Was Chosen
List of dictionaries is easy to manage and extend compared to complex database systems.
sorted() with lambda provides flexibility without writing long functions.
Tuple sorting helps handle multiple conditions efficiently.
This approach keeps the code simple, readable, and suitable for a beginner-level project.
Possible Improvements
Store leaderboard in a file (CSV/JSON) instead of memory
Add user input to choose sorting type (ascending/descending)
Format output in a table for better readability
Conclusion
This project enhances the basic number guessing game by adding a leaderboard system. By implementing sorting and display features, the program becomes more interactive and useful. The chosen approaches ensure simplicity while maintaining flexibility for future improvements.
Comments
Post a Comment