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

Above Average

To finish, two more lines below the leaderboard: the average score of a game, with one decimal place, and the players whose best score is above that average:

4. Max 60
Average score: 105.7
Above average: Eva, Anna, Tom

The average is the total points divided by the number of games, and your program already knows both: 740 / 7 is 105.714…, which is 105.7 with one decimal place. Eva's, Anna's and Tom's best scores are higher than that, Max's 60 isn't. List the players above the average in the order of the leaderboard, with a comma and a space between the names.

A tip: the players above the average are the leaderboard's entries that meet a condition, and of those only the names: a job for a list comprehension with if.

That's the whole leaderboard. From a list of messy texts, your program has made a clean list of players, a leaderboard of their best scores and the average, using text methods, a set, a dictionary, sorting with your own key function and a list comprehension. But what if the game saves a broken result, such as "max:sixty"? int() can't make a number out of "sixty", so the program ends with ValueError: invalid literal for int() with base 10: 'sixty'. In the next lesson, Errors and Exceptions, you'll learn to catch such an error, so that the program can skip the broken result and go on.

Instructions

  1. 1

    Print the average score of all the games, with one decimal place.

  2. 2

    Print the players whose best score is above the average, in the order of the leaderboard.

Back

3/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.