OpenSesame
Rapunzel Code Editor
DataMatrix
Support forum
Python Tutorials
MindProbe
Python videos

Iterables: Exercises and Solutions

Best-selling artists

Exercise

Define an artists dict with the names of the four best-selling music artists as keys, and their claimed sales as values:

  1. The Beatles with 600 million sales
  2. Elvis Presley with 600 million sales
  3. Michael Jackson with 350 million sales
  4. Madonna with 300 million sales

Then ask the user to enter the name of an artist. Look up the number of sales of this artist, falling back to 'unknown' if the artists is not in the dict, and print out the result.

Solution

artists = {
  'The Beatles': 600e6,
  'Elvis Presley': 600e6,
  'Michael Jackson': 350e6,
  'Madonna': 300e6
}
artist = input('Enter an artist name: ')
sold = artists.get(artist, 'unknown')
print('{0} has sold {1} records'.format(artist, sold))

Output:

Enter an artist name: Rick Ross
Rick Ross has sold unknown records

You're done with this section!

Continue with Iterables >>