title: Getting Traceback and UnboundLocalError
body: <p>Getting the following errors on my Python script that is supposed to read an CSV file with ticker symbols and price targets. To execute the script I need to type <code>python nameofscript.py nameoftickers.csv</code> as the CSV passes into the script.</p>
  
  <p>The script’s logic is fairly simple: it takes a text file as an argument and reads the stock symbol, trigger value and the condition ( a for above and b for below ) from each line in the file. If the condition is met, then it sends an email to a predetermined email address and sends a push notification to your cellphone (in this case, an iPhone) through Pushover.</p>
  
  <pre><code>jovan@jovan-VirtualBox:~/pythonprojects$ python emailalerts.py symbols.csv 
  Opening Google Finance URL...
  Checking quotes for AAPL
  Traceback (most recent call last):
    File "emailalerts.py", line 74, in &lt;module&gt;
      quote = quote_grab(linelst[0])
    File "emailalerts.py", line 30, in quote_grab
      return price #returns price as a float
  UnboundLocalError: local variable 'price' referenced before assignment
  </code></pre>
  
  <p>The python script is below. I'm confused about the Traceback error but also the local variable. I've defined 'price' in the script.</p>
  
  <pre><code>import string, re, os, time, smtplib, sys
  from urllib import urlopen
  import httplib, urllib #used in the Pushover code
  
  
  def quote_grab(symbol):
  
      baseurl = 'http://google.com/finance?q='
      urlData = urlopen(baseurl + symbol)
  
      print 'Opening Google Finance URL...'
  
      # Another option: namestr = re.compile('.*name:\"' + symbol + '\",cp:(.*),p:(.*?),cid(.*)}.*')
      namestr = re.compile('.*name:\"' + symbol + '\",cp:(.*),p:(.*?),cid(.*)') # "?" used as there is a second string "cid" in the page and the Match was being done up to that one. The "?" keeps it to the 1st occurrence.
  
      print 'Checking quotes for ' + symbol
  
      for line in urlData:
  
          m = re.match(namestr, line)
  
          if m:
              #Since the method m.group(2) returns a string in the form "xxxx", it cannot be converted to float,
              #therefore I strip the "" from that string and pass it to the float function.
              priceStr = m.group(2).strip('"')
              price = float(priceStr)
  
  
      urlData.close()
      return price #returns price as a float
  
  def pushover(msg):
  
      conn = httplib.HTTPSConnection("api.pushover.net:443")
      conn.request("POST", "/1/messages.json",
          urllib.urlencode({
              "token": "________",
              "user": "________",
              "message": msg,
      }), { "Content-type": "application/x-www-form-urlencoded" })
      conn.getresponse()
  
  
  def send_email(sbjt, msg):
      fromaddr = '________'
      toaddrs = '________'
      bodytext = 'From: %s\nTo: %s\nSubject: %s\n\n%s' %(fromaddr, toaddrs, sbjt, msg)
  
      # Credentials (if needed)
      username = '________'
      password = '________' 
  
      # The actual mail sent
      server = smtplib.SMTP('smtp.gmail.com:587')
      server.starttls()
      server.login(username,password)
      server.sendmail(fromaddr, toaddrs, bodytext)
      server.quit()
  
  #------------------------
  # Constants
  file = sys.argv[1]
  
  #------------------------
  
  # Opens .cvs file, gets string at last line, converts it to list so that the comparison in the
  # IF statement below can be done
  csvFile = open(file, 'r')
  body = 'Changes:\n'
  chg = False
  
  for line in csvFile:
      linelst = line.split(',')
      quote = quote_grab(linelst[0])
  
      if quote&gt;float(linelst[1]) and linelst[2]==('a\n' or 'a'):
          body = body + 'Price for %s went up to %s (threshold = %s)\n' % (linelst[0], quote, linelst[1])
          chg = True
      if quote&lt;float(linelst[1]) and linelst[2]==('b\n' or 'b'):
          body = body + 'Price for %s went down to %s (threshold = %s)\n' % (linelst[0], quote, linelst[1])
          chg = True
  
  if chg:
      print 'sending email...'
      send_email('Stock Price Changes',body)
      print 'sending message to pushover...'
      pushover(body)
  
  csvFile.close()
  </code></pre>