The following example demonstrates a program that uses 3 levels of nested dictionaries to create a simple music library. The following program uses nested dictionaries to store a small music library. Extend the program such that a user can add artists, albums, and songs to the library. First, add a command that adds an artist name to the music dictionary. Then add commands for adding albums and songs. Take care to check that an artist exists in the dictionary before adding an album, and that an album exists before adding a song.

music = {
'Pink Floyd': {
'The Dark Side of the Moon': {
'songs': [ 'Speak to Me', 'Breathe', 'On the Run', 'Money'],
'year': 1973,
'platinum': True
},
'The Wall': {
'songs': [ 'Another Brick in the Wall', 'Mother', 'Hey you'],
'year': 1979,
'platinum': True
}
},
'Justin Bieber': {
'My World':{
'songs': ['One Time', 'Bigger', 'Love Me'],
'year': 2010,
'platinum': True
}
}
}

#complete/edit code given below:

# Get user input

# While user input != 'exit'
# ...

Respuesta :

Answer:

def add_album(artist,album):

   song_list = input("enter the list of song titles separated by comma: ").split(",")

   print(song_list)

   year = int(input("enter release year: "))

   is_platinum = bool(input("Is it platinum? True/False"))

   music[artist][album] = {}

   music[artist][album]['songs'] = song_list

   music[artist][album]['year'] = year

   music[artist][album]['platinum'] = is_platinum

def add_artist():

   artist = input("enter name of artist: ")

   if music.get(artist, "no") == "no":

       music[artist] = {}

       

   else:

       print("artist already exist in dictionary.")

   is_album = input("add album? ")

   if is_album == "yes":

       album_name = input("Enter album name: ")

       add_album(artist,album_name)

           

for _ in iter(dict, 0):

   is_exist = input("Do you want to end the program? yes/no: ")

   if is_exist == "yes":

       break

   elif is_exist == "no":

       is_art = input("Add artist? ")

       if is_art == "yes":

           add_artist()

       else:

           print("enter  yes/no")

   else:

       print("make a decision, yes/no")

Explanation:

The python program defines an "add_artist" and "add_album" function that references the dictionary "music". The functions prompt for user inputs of the artist name, album, year, and platinum record.

The functions use the bracket notation to access and update the dictionary in the memory.

ACCESS MORE