๐ฎ CA 03 โ Number Guessing Game Leaderboard (Python)
Hi All,
In this task, I implemented a Leaderboard System for a Number Guessing Game using Python.
๐ Problem Statement
- Provide an option to view leaderboard details.
- Sort the leaderboard based on:
- Difficulty (Easy, Medium, Hard)
- Attempts (Ascending / Descending)
๐ก Approach
- Used a list of dictionaries to simulate a database.
- Created separate functions:
-
show_leaderboard()โ to display data -
sort_leaderboard()โ to sort data
-
- Used:
-
sorted()function -
lambdaexpressions - Custom mapping for difficulty sorting
-
- Implemented a menu-driven program for user interaction.
๐๏ธ Leaderboard Data
leaderboard = [
{"name": "manoj", "difficulty": "Easy", "attempts": 3},
{"name": "rahul", "difficulty": "Hard", "attempts": 7},
{"name": "anita", "difficulty": "Medium", "attempts": 5}
]
๐ Display Leaderboard Function
def show_leaderboard():
print("\n--- Leaderboard ---")
for player in leaderboard:
print(f"Name: {player['name']}, Difficulty: {player['difficulty']}, Attempts: {player['attempts']}")
๐ Sorting Function
def sort_leaderboard():
print("\nSort by:")
print("1. Difficulty")
print("2. Attempts")
choice = int(input("Enter choice: "))
order = input("Ascending or Descending (a/d): ").lower()
reverse = True if order == 'd' else False
if choice == 1:
order_map = {"Easy": 1, "Medium": 2, "Hard": 3}
sorted_list = sorted(leaderboard, key=lambda x: order_map[x["difficulty"]], reverse=reverse)
elif choice == 2:
sorted_list = sorted(leaderboard, key=lambda x: x["attempts"], reverse=reverse)
print("\n--- Sorted Leaderboard ---")
for player in sorted_list:
print(f"Name: {player['name']}, Difficulty: {player['difficulty']}, Attempts: {player['attempts']}")
๐งญ Menu-Driven Program
while True:
print("\n1. View Leaderboard")
print("2. Sort Leaderboard")
print("3. Exit")
option = input("Enter option: ")
if option == "1":
show_leaderboard()
elif option == "2":
sort_leaderboard()
elif option == "3":
break
else:
print("Invalid option")
๐ฅ๏ธ Sample Output
1. View Leaderboard
2. Sort Leaderboard
3. Exit
Enter option: 1
--- Leaderboard ---
Name: manoj, Difficulty: Easy, Attempts: 3
Name: rahul, Difficulty: Hard, Attempts: 7
Name: anita, Difficulty: Medium, Attempts: 5
Enter option: 2
Sort by:
1. Difficulty
2. Attempts
Enter choice: 2
Ascending or Descending (a/d): a
--- Sorted Leaderboard ---
Name: manoj, Difficulty: Easy, Attempts: 3
Name: anita, Difficulty: Medium, Attempts: 5
Name: rahul, Difficulty: Hard, Attempts: 7
๐ง Explanation
-
leaderboardstores player details like a database. -
show_leaderboard()prints all records. -
sort_leaderboard():- Sorts using
sorted() - Uses
lambdato define sorting key - Uses
reverse=Truefor descending order
- Sorts using
- Custom mapping is used to sort difficulty logically:
- Easy โ Medium โ Hard
โ Conclusion
This task helped me understand:
- Data storage using lists and dictionaries
- Sorting techniques in Python
- Writing structured and modular code
๐ This can be further improved by connecting to a real database or integrating with a full game.
Top comments (0)