Advertisement
Guest User

Untitled

a guest
May 31st, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #! /usr/bin/python3
  2. # music_inventory.py -> Counts the number of records of specific artists given to Mike for his birthday
  3.  
  4. import pprint
  5.  
  6. # Create a nested dictionary for the music inventory
  7.  
  8. birthday_inventory = { 'Jere': {'Beatles': 4, 'Pink Floyd': 3, 'Bob Dylan': 2},
  9. 'Sally': {'Taylor Swift': 8},
  10. 'Luke': {'U2': 10, 'Marvin Gaye': 3, 'Stevie Wonder': 2, 'Beatles': 5},
  11. 'David': {'Justin Bieber': 10, 'Shawn Mendes': 1},
  12. 'Molly': {'Beatles': 6, 'Bob Dylan': 1}}
  13.  
  14. # Create a function that prints out the total of a specific artist
  15.  
  16. def findTotal(inventory, artist):
  17. record_number = 0 # Set the default number of the specific artist to be zero
  18. for k, v in inventory.items(): # Searching through the key-value pairs to find the nested dictionary
  19. record_number = record_number + v.get(artist, 0) # Add the number of the specific artist; if it isn't there, just add zero
  20. return record_number
  21.  
  22. # Create a function to print out all of the items and frequency in a loop (allows you to use any nested dictionary)
  23.  
  24. def printItems(inventory):
  25. for k, v in inventory.items(): # Search in the initial key-value pairs
  26. for i in v: # Prints out all of the artists
  27. record_total = findTotal(inventory, i)
  28. print(record_total)
  29.  
  30. printItems(birthday_inventory)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement