title: Python variable error - reference before assignment
body: <p>I have written a program that gets the information of a server. However, the program says that <code>leng</code> is referenced before assignment. How can I prevent this?</p>
  
  <p>The program first scans for open ports. Then it connects to the first open port it finds. It then gets information about the server. The program then gets the IP address of the program and finally it closes the connection.</p>
  
  <pre><code>import socket
  
  VER = None
  
  portlist = [21,22,25,80,110,443]
  
  ports_test = [21,22,25,80,110,443]
  
  length = 0
  
  leng = 0
  
  siteORip = input("input either a site &lt;eg:www.anysite.com&gt; or an ip address&lt;eg:123.45.67.890&gt;&gt;")
  
  def get_ban():
      print("[CONNECTING&gt;]Establishing connection...")
      while True:
          try:
              port = portlist.pop(0)
              s = socket.socket()
              socket.setdefaulttimeout(5)
              s.connect((siteORip,port))
              s.send("x".encode())
              data = s.recv(1024)
              print("[SERVER BANNER&gt;]",data)
              break
          except:
              s.close()
              length = length + 1
              if length == 6:
                  print("[ERROR&gt;]Failed to connect")
                  break
              else:
                  continue
  
  def get_info():
      try:
          info = socket.gethostbyaddr(siteORip)
          print("[SERVER INFO:([HOST,ADDITIONAL HOST,IP ADDRESS])&gt;]",info)
      except:
          print("[ERROR&gt;]Unknown error retrieving server info")
  
  def get_ip():
      try:
          IP = socket.gethostbyname(siteORip)
      except:
          IP = socket.gethostbyname(siteORip)
      try:
          print("[IP ADDRESS&gt;]",IP)
      except:
          print("[ERROR&gt;]Unable to access IP")
      VER = 1
      s.close()
  
  def ports_open():
      while True:
          try:
              for x in ports_test:
                  s = socket.socket()
                  socket.setdefaulttimeout(5)
                  s.connect((siteORip,x))
                  print("[TESTING...&gt;]Port",x,"is open")
                  s.close()
          except:
              print("[TESTING...&gt;]Port",x,"is closed")
              s.close()
              leng = leng + 1
              if leng == 6:
                  break
              else:
                  continue
  
  def main():
      ports_open()
      get_ban()
      get_info()
      get_ip()
      print("[&lt;PROGRAM CONNECTION ENDED&gt;]")
  
  main()      
  wait = input("")    
  </code></pre>