Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PYTHON The following program uses nested dictionaries to store a small music lib

ID: 3766311 • Letter: P

Question

PYTHON

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'
# ...

Explanation / Answer

Here you go

# your code goes here
# your code goes here
print "Enter artist name:",
artist = raw_input()
music = {};

while artist != 'exit'
{
   #check if artist already exists or not
   if artist in music.keys:
       print "Artist Already exists"
   else
       music[artist] = {}
  
   print "Enter Album name: "
   album = raw_input()
  
   #check if album exists
   if album in music[artist].keys:
       print "Album already there"
       print "Enter song: "
       song = raw_input()
       music[artist][album]['songs'].append(song)
   else
       music[artist][album] = {}
  
       #now get the values
       print "Enter song: "
       song = raw_input()
       music[artist][album]['songs'] = song
      
       print "Enter year: "
       year = raw_input()
       music[artist][album]['year'] = year
      
       print "Is platinum(True/False)?: "
       platinum = raw_input()
       music[artist][album]['platinum'] = platinum
}