"""File: musician_d.py Purpose: Store information on a single musician as a Python dictionary Run: python musician_d.py Input: Name of musician, songs. For each song, name, year, runtime. Commands Output: The input information, results of commands. """ #---------------------------------------------------------------------- def create_song(song_title): """Get a song from the user and put it in a dictionary""" song = dict() song['Title'] = song_title year = int(raw_input("Enter the song's year\n ")) song['Year'] = year runtime = int(raw_input("Enter the song's run time\n ")) song['Run time'] = runtime return song #---------------------------------------------------------------------- def build_song_dict(): """Build a dictionary storing the songs""" songs = dict() print "Enter the songs" title = raw_input("Enter first song title (empty line to stop)\n ") while title != "": song = create_song(title) songs[title] = song title = raw_input("\nEnter next song title (empty line to stop)\n ") # print "songs =", songs return songs #---------------------------------------------------------------------- def create_musician_info(): """Store information on a musician as a dictionary""" new_musician = dict() name = raw_input("What's the name of the musician or group?\n ") new_musician["Name"] = name songs = build_song_dict() new_musician["Songs"] = songs return new_musician #---------------------------------------------------------------------- def print_song(song): """Print info on a single song""" print song["Title"], " ", print song["Year"], " ", print song["Run time"] #---------------------------------------------------------------------- def print_musician(musician): """Print info on a musician that's stored as a dictionary""" print print "Musician:" print " Name: ", musician["Name"] print " Songs:" for title in musician["Songs"]: print " ", song = musician["Songs"][title] print_song(song) #---------------------------------------------------------------------- def menu(): """Print a menu of options""" print "You can:" print " Add a song to the database (a)" print " Delete a song from the database (d)" print " Print information on a song (p)" print " Print this menu (m)" print " Quit (q)" #---------------------------------------------------------------------- def add_song(musician): """Add a song to the database""" title = raw_input("What's the song's title?\n ") song = create_song(title) musician["Songs"][title] = song #---------------------------------------------------------------------- def delete_song(musician): """Delete a song from the database""" title = raw_input("What's the song's title?\n ") if musician["Songs"].has_key(title): del musician["Songs"][title] else: print title, "isn't in the database" #---------------------------------------------------------------------- def print_song_gt(musician): """Get a song's title and then print the information on it""" title = raw_input("What's the song's title?\n ") if musician["Songs"].has_key(title): song = musician["Songs"][title] print_song(song) else: print title, "isn't in the database" #---------------------------------------------------------------------- def update_query(musician): menu() cmd = raw_input("Enter a command (a, d, p, m, q)\n ") while cmd != "q" and cmd != "Q": if cmd == "a" or cmd == "A": add_song(musician) elif cmd == "d" or cmd == "D": delete_song(musician) elif cmd == "p" or cmd == "P": print_song_gt(musician) elif cmd == "m" or cmd == "M": menu() else: print cmd, "isn't a valid command. Please try again" menu() cmd = raw_input("Enter a command (a, d, p, m, q)\n ") #---------------------------------------------------------------------- # Main program # Only execute the main program if this file is *not* imported into another if __name__ == "__main__": musician = create_musician_info() print_musician(musician) update_query(musician)