Loops: Exercises and Solutions
Best-selling artists
Exercise
Do the following until the user enters quit
:
- Ask the name of an artist
- Look up the number of sales of this artist in a
dict
- Print out the result if the number of sales are known
- If the number of sales are unknown, ask the user to enter the number of sales, and update the
dict
accordingly
Solution
sales = {}
while True:
artist = input('Enter an artist name: ')
if artist == 'quit':
break
if artist in sales:
print('{0} has sold {1} records'.format(artist, sales[artist]))
continue
sold = input('Enter sales: ')
sales[artist] = sold
Output:
Enter an artist name: Michael Jackson
Enter sales: 350000000
Enter an artist name: Michael Jackson
Michael Jackson has sold 350000000 records
Enter an artist name: quit
You're done with this section!
Continue with loops >>