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