title: Generating Random Combinations of Dictionary Key and Value Sets
body: <p>I am creating a program to sort through a music library file. I have this one particular function, generateRandomPlaylist(musicLibDictionary), that I am stuck on.</p>
  
  <p>The function needs to randomly pick a key from the dictionary and randomly assign one of the values to the key. For instance the artists in the dictionary include The Who, Adele and The Beatles. The respective albums include Tommy; 19, 21, 25; Abbey Road, Let It Be. I need the program to randomly pick one of the keys (the artists) and then randomly pick one of the key's values. The randomly generated playlist needs to have all three artists, not repeats, but different albums from the artist. The way I have it set up, the output doesn't necessarily use all three artists. Sample output should look like:</p>
  
  <pre><code>Here is your random playlist:
  - 25 by Adele
  - Abbey Road by The Beatles
  - Tommy by The Who
  </code></pre>
  
  <p>Each time the function is called, the playlist should be different. Like I said, right now the function doesn't run properly, and I also get a printing error saying I cannot concatenate a list and a string, so I don't know where I am going wrong. </p>
  
  <p>The code for the program is below:</p>
  
  <pre><code># importing pickle
  import pickle
  import random
  
  # declaration of functions
  def displayMenu():
      print("Welcome to Your Music Library")
      print("Options:")
      print("\t1) Display Library")
      print("\t2) Display all artists")
      print("\t3) Add an album")
      print("\t4) Delete an album")
      print("\t5) Delete an artist")
      print("\t6) Search Library")
      print("\t7) Generate a random playlist")
      print("\t8) Make your own playlist")
      print("\t9) Exit")
  
  def displayLibrary(musicLibDictionary):
      for key in musicLibDictionary:
          print("Artist: " + key)
          print("Albums: ")
          for album in musicLibDictionary[key]:
              print("\t- " + album)
  
  def displayArtists(musicLibDictionary):
      print("Displaying all artists:")
      for key in musicLibDictionary:
          print(" - " + key)
  
  def addAlbum(musicLibDictionary):
      artistName = input("Please enter the name of the artist you would like to add: ")
      albumName = input("Please enter the name of the album you would like to add: ")
      if artistName in musicLibDictionary.keys():
          musicLibDictionary[artistName].append(albumName)
      else:
          musicLibDictionary[artistName] = [albumName]
  
  def deleteAlbum(musicLibDictionary):
      artist = input("Enter artist: ")
      albumToBeDeleted = input("Enter album: ")
      if artist in musicLibDictionary.keys():
          if albumToBeDeleted in musicLibDictionary[artist]:
              musicLibDictionary[artist].remove(albumToBeDeleted)
              return True
          else:
              return False
      else:
          return False
  
  def deleteArtist(musicLibDct):
      artistToBeDeleted = input("Enter artist to delete: ")
      if artistToBeDeleted in musicLibDct.keys():
          del musicLibDct[artistToBeDeleted]
          return True
      else:
          return False
  
  def searchLibrary(musicLibDictionary):
      searchTerm = input("Please enter a search term: ")
      searchTerm = searchTerm.lower()
      print("Artists containing" + searchTerm)
      for key in musicLibDictionary.keys():
          if searchTerm.lower() in key.lower():
              print("\t - ", end="")
              print(key)
      print("Albums containing" + searchTerm)
      for album in musicLibDictionary[key]:
          print("\t- " + album)
  
      for key in musicLibDictionary.keys():
          for album in musicLibDictionary[key]:
              if searchTerm in album.lower():
                  print("\t - ", end="")
                  print(album)
  
  def generateRandomPlaylist(musicLibDictionary):
      print("Here is your random playlist:")
      for artist in musicLibDictionary.keys():
          artistSelection = random.choice(list(musicLibDictionary.keys()))
          albumSelection = random.choice(list(musicLibDictionary.values()))
      print("\t-" + albumSelection + "by" + artistSelection)
  
  
  def loadLibrary(libraryFileName):
      fileIn = open(libraryFileName, "rb")
      val = pickle.load(fileIn)
      val = dict(val)
      return val
  
  def saveLibrary(libraryFileName, musicLibDictionary):
      fileIn = open(libraryFileName, "wb")
      pickle.dump(musicLibDictionary, fileIn)
  
  def main():
      musicLib = loadLibrary("musicLibrary.dat")
      userChoice = ""
      while (userChoice != 7):
          displayMenu()
          userChoice = int(input("&gt; "))
          if userChoice == 1:
              displayLibrary(musicLib)
          elif userChoice == 2:
              displayArtists(musicLib)
          elif userChoice == 3:
              addAlbum(musicLib)
          elif userChoice == 4:
              deleteAlbum(musicLib)
          elif userChoice == 5:
              deleteArtist(musicLib)
          elif userChoice == 6:
              searchLibrary(musicLib)
          elif userChoice == 7:
              generateRandomPlaylist(musicLib)
          elif userChoice == 8:
              saveLibrary("musicLibrary.dat", musicLib)
  
  
  # Call main
  main()
  </code></pre>