DEV Community

Manoj Kumar
Manoj Kumar

Posted on

CA 03 โ€“ Number Guessing Game Leaderboard (Python)

๐ŸŽฎ 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

  1. Provide an option to view leaderboard details.
  2. 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
    • lambda expressions
    • 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}
]
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“Š Display Leaderboard Function

def show_leaderboard():
    print("\n--- Leaderboard ---")
    for player in leaderboard:
        print(f"Name: {player['name']}, Difficulty: {player['difficulty']}, Attempts: {player['attempts']}")
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”„ 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']}")
Enter fullscreen mode Exit fullscreen mode

๐Ÿงญ 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")
Enter fullscreen mode Exit fullscreen mode

๐Ÿ–ฅ๏ธ 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
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  Explanation

  • leaderboard stores player details like a database.
  • show_leaderboard() prints all records.
  • sort_leaderboard():
    • Sorts using sorted()
    • Uses lambda to define sorting key
    • Uses reverse=True for descending order
  • 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)