PYTHON Extend the program such that a user can add artists, albums, and songs to
ID: 3764219 • Letter: P
Question
PYTHON
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
}
}
}
# Get user input
# While user input != 'exit'
# ...
Explanation / Answer
music = {}
valid = False;
while(valid == False):
artist = input ("Please enter artist name:")
if(artist not in music):
print(" Key not exist");
music[artist] = {}
#Now ask other details
album = input ("Please enter album name:")
if(album not in music[artist]):
print(" album not exist");
music[artist][album] = {}
print("Add song(s) in new line (Exit to stop)")
song = ""
if("songs" not in music[artist][album]):
music[artist][album]["songs"] = []
songarr = (music[artist][album]["songs"])
while(song != "Exit" and song != "exit"):
song = input ("Please enter song name:")
if((song != "Exit" and song != "exit") and song not in songarr):
songarr.append(song)
music[artist][album]["songs"] = songarr
year = input("Enter year of album")
music[artist][album]["year"] = year
platinum = input("Is it Platinum ? (y/n)")
if(platinum == "y"):
music[artist][album]["platinum"] = True
else:
music[artist][album]["platinum"] = False
print(music)
uinput = input("Are you continuing (y/n) ?")
if(uinput == "y"):
valid = True
-----------output------------------
Please enter artist name:Pink Floyd
Key not exist
Please enter album name:The Dark Side of the Moon
album not exist
Add song(s) in new line (Exit to stop)
Please enter song name:Speak to Me
Please enter song name:Breathe
Please enter song name:exit
Enter year of album1973
Is it Platinum ? (y/n)y
{'Pink Floyd': {'The Dark Side of the Moon': {'platinum': True, 'songs': ['Speak to Me', 'Breathe'], 'year': '1973'}}}
Are you continuing (y/n) ?n
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.