Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

The Best Scores

Now the leaderboard itself. Most players have played more than once, and on the leaderboard, each of them gets one line with their best score. Eva scored 140 and later 85, so she's on the leaderboard with 140. Below the players, print the heading Leaderboard: and then the players from the highest best score to the lowest, numbered from 1:

Players: Anna, Eva, Max, Tom
Leaderboard:
1. Eva 140
2. Anna 130
3. Tom 110
4. Max 60

A tip: a dictionary can keep the best scores, with a cleaned-up name as the key and that player's best score as the value. Go through the games: a player who isn't in the dictionary yet goes in with the score of that game, and a player who is gets a new value only when the game's score is higher. The leaderboard is then the dictionary's pairs, .items(), sorted by the score from the highest. Without a key, sorted() sorts pairs by their first value, which here is the name, so it needs a key function of your own that returns the score. And enumerate() numbers the lines.

Here too, the exercise tries other data, such as a player who climbs to the top and a new player.

Instructions

  1. 1

    Print the leaderboard below the players.

Back

2/3

  1. 1. The Players
  2. 2. The Best Scores
  3. 3. Above Average
Next

Try it yourself

Do this exercise right here in your browser. No account needed.