title: local variable 'data' referenced before assignment - python
body: <p>I am getting the error in my code</p>
  <blockquote>
  <p>Request Method:   GET<br />
  Request URL:  http://localhost:8000/emotion/<br />
  Django Version:   2.2<br />
  Exception Type:   UnboundLocalError<br />
  Exception Value:<br />
  local variable 'data' referenced before assignment<br />
  Exception Location:   C:\Users\Sant\Desktop\music_demo\music_site\views.py in emotion, line 314<br />
  Python Executable:    C:\Users\Sant\AppData\Local\Programs\Python\Python37-32\python.exe<br />
  Python Version:   3.7.3<br />
  Python Path:<br />
  ['C:\Users\Sant\Desktop\music_demo',</p>
  <p>Server time:  Sun, 16 Jun 2019 17:27:51 +0000</p>
  </blockquote>
  <p>Code:</p>
  <pre class="lang-py prettyprint-override"><code>def emotion(request):
      from mutagen.id3 import ID3
      from mutagen.mp3 import MP3
      import sys
      import spotipy
      from spotipy.oauth2 import SpotifyClientCredentials
  
      client_id = '26473c91fefc43eca3a6531e0f062723'
      client_secret = '9d7c8ddb18594838ae5db6ad10b3ddf0'
      title = 'lollypop'
      artist = 'pawan singh'
  
      client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
      sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
      sp.trace=False
      search_query = title+ ' ' + artist
      result = sp.search(search_query)
      for i in result['tracks']['items']:
          # Find a songh that matches title and artist
          if (i['artists'][0]['name'] == artist) and (i['name'] == title):
              print (i['uri'])
              break
      else:
          try:
              # Just take the first song returned by the search (might be named differently)
              print (result['tracks']['items'][0]['uri'])
              uri = result['tracks']['items'][0]['uri']
              client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
              sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
              sp.trace=False
              features = sp.audio_features(uri)
  
              print ('Energy:', features[0]['energy'])
              data = {
                  &quot;energy&quot; :  features[0][&quot;energy&quot;],
                  &quot;valence&quot; : features[0][&quot;valence&quot;],
                  &quot;key&quot; : features[0][&quot;key&quot;],
                  &quot;link&quot; : features[0][&quot;valence&quot;],
                  &quot;danceability&quot; : features[0][&quot;link&quot;],
                  &quot;loudness&quot; : features[0][&quot;loudness&quot;],
                  &quot;tempo&quot; : features[0][&quot;tempo&quot;],
                  &quot;acousticness&quot; : features[0][&quot;acousticness&quot;],
                  &quot;liveness&quot; : features[0][&quot;liveness&quot;],
                  &quot;instrumentalness&quot; : features[0][&quot;instrumentalness&quot;]
              }
  
          except:
              # No results for artist and title
              print (&quot;Cannot Find URI&quot;)
      # return HttpResponse(features[0]['energy'])
      return render(request, 'music_site/emotion.html', data) # Error is raised because of data dictionary
  </code></pre>