title: NameError says variable is not defined, but only in some places
body: <p>I am trying to implement a keep-alive that sends some data every 30 seconds to keep a telnet connection open.</p>
  
  <p>My code calls <code>reinitScore</code> every second.  This function will sometimes call <code>calculateWinner</code>, which sends the data through telnet via <code>stelnet.send(data)</code>.</p>
  
  <p>The problem is, when I call <code>stelnet.send(data)</code> inside any function, it raises a <code>NameError: global name 'stelnet' is not defined</code>.</p>
  
  <p>My questions is: why would <code>stelnet.send(data)</code> work in one place, and not another?</p>
  
  <p>Here is the part of my code that concerns telnet transfer and function calling: </p>
  
  <pre><code>import socket, select, string, sys
  import string
  import threading
  
  leftKeyCounter = 0
  rightKeyCounter = 0
  frontKeyCounter = 0
  backKeyCounter = 0
  
  # function called by reinitScore
  def calculateWinner(d):
      scores = {}
      high_score = 0
      for key, value in d.items():
          try:
              scores[value].append(key)
          except KeyError:
              scores[value] = [key]
          if value &gt; high_score:
              high_score = value
      results = scores[high_score]
      if len(results) == 1:
          print results[0]
          stelnet.send(results[0])
          return results[0]
      else:
          print 'TIE'
          return 'TIE', results
  
  #called once and repeat itselfs every second
  def reinitScore():
      threading.Timer(1, reinitScore).start()
      #globaling for changing the content
      global leftKeyCounter
      global rightKeyCounter
      global frontKeyCounter
      global backKeyCounter
      values = {'left' : leftKeyCounter, 'right' : rightKeyCounter, 'front' : frontKeyCounter, 'back' : backKeyCounter}
      if (leftKeyCounter != 0 or rightKeyCounter != 0 or frontKeyCounter != 0 or backKeyCounter != 0):
          calculateWinner(values)
          leftKeyCounter = 0
          rightKeyCounter = 0
          frontKeyCounter = 0
          backKeyCounter = 0
          print "back to 0"
  
  reinitScore()
  if __name__ == "__main__":
  
      if(len(sys.argv) &lt; 3) :
  
  
      print 'Usage : python telnet.py hostname port'
      sys.exit()
  
      host = sys.argv[1]
      port = int(sys.argv[2]) 
      stelnet = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      stelnet.settimeout(2)
  
      # connect to remote host
      try :
          stelnet.connect((host, port))
      except :
          print 'Unable to connect'
          sys.exit()
  
      print 'Connected to remote host'
  
      while True:
         // ... Some code that has nothing to do with telnet 
  
      while 1:
         socket_list = [sys.stdin, stelnet]
  
          read_sockets, write_sockets, error_sockets = select.select(socket_list , [], [])
  
          for sock in read_sockets:
              if sock == stelnet:
                  data = sock.recv(4096)
                  if not data :
                      print 'Connection closed'
                      sys.exit()
                  else :
                      sys.stdout.write(data)
  
              else :
                  msg = sys.stdin.readline()
                  stelnet.send(msg)
  </code></pre>
  
  <p>I tried to declare <code>stelnet</code> as a <code>global</code> variable at many places, but it doesn't change anything --- I always get the "not defined" <code>NameError</code>.</p>