title: Local variables and elif
body: <p>I would like to know why the compiler writes: </p>
  
  <blockquote>
    <p>local variable 'randomindexmother1' referenced before assignment </p>
  </blockquote>
  
  <p>I have been searching and I do not need to treat the randomindexfather1 as a global variable. Therefore, I think that is a mater of the elif statements.</p>
  
  <pre><code>def generation(listPaths):
      # This function will give us the new generation of kids
      # we will introduce the survived paths
      # This function will take as input the list sorted paths and sorted fitnesses of each path
      # listP will be a list produced in a while statmentself.
      rand1 = 0
      rand2 = 0
      lunusedindex = []
      newpaths = []
      i = 0
      fpoint = listPaths[0][0][0]
      lpoint = listPaths[0][0][-1]
      mother = []
      father = []
      kid1 = []
      srtlist = []
      # this is going to be the random number between the fitness of the maximum path fitness and the minimum of
      # the half population
      leng = len(listPaths)
      lunusedindex = range(leng)
      while i &lt; leng / 2:
          # here we select an aleatory father and an aleatory mother
          rand1 = random.choice(lunusedindex)
          rand2 = random.choice(lunusedindex)
          if rand1 == rand2:
              while rand1 == rand2:
                  rand1 = random.choice(lunusedindex)
          # here we delete the mother and the father selected to not repeat a father and mother
          del lunusedindex[rand1]
          if rand1 &lt; rand2:
              del lunusedindex[rand2 - 1]
          else:
              del lunusedindex[rand2]
          k = 0
          while k &lt; 2:
              mother = copy.deepcopy(listPaths[rand1][0])
              father = copy.deepcopy(listPaths[rand2][0])
              del mother[0]
              del mother[-1]
              del father[0]
              del father[-1]
              indexfather = range(len(father))
              indexmother = range(len(mother))
              kid1.append(fpoint)
              j = 0
              ref = 0
              rem = 0
              while j &lt; len(father) or len(mother):
                  # with this statment we ensuring that less points are going to be repeated and if mother or father
                  # become empty, this means that all the points are used but some of them can be repeated
                  if len(indexfather) &lt;= 2:
                      randomindexfather1 = random.randint(indexfather[0], indexfather[1 - ref])
                      del indexfather[0:randomindexfather1 + 1]
                      ref += 1
                      if len(indexfather) == 0:
                          randomindexfather2 = randomindexfather1 + 1
                      else:
                          randomindexfather2 = random.randint(indexfather[0], indexfather[1 - ref])
  
                  elif len(indexmother) &lt;= 2:
                      randomindexmother1 = random.randint(indexmother[0], indexmother[1 - rem])
                      del indexmother[0:randomindexmother1 + 1]
                      rem += 1
                      if len(indexmother) == 0:
                          randomindexmother2 = randomindexmother1 + 1
                      else:
                          randomindexmother2 = random.randint(indexmother[0], indexmother[1 - rem])
  
                  elif len(indexfather) &gt; 2:
                      randomindexfather1 = random.randint(indexfather[0], (len(indexfather) - 2))
                      del indexfather[0:randomindexfather1 + 1]
                      randomindexfather2 = random.randint(indexfather[0], (len(indexfather) - 1))
  
                  elif len(indexmother) &gt; 2:
                      randomindexmother1 = random.randint(indexmother[0], (len(indexmother) - 2))
                      del indexmother[0:randomindexmother1 + 1]
                      randomindexmother2 = random.randint(indexmother[0], (len(indexmother) - 1))
  
                  kid1.extend(father[randomindexfather1:randomindexfather2])
                  kid1.extend(mother[randomindexmother1:randomindexmother2])
                  del father[randomindexfather1:randomindexfather2]
                  del mother[randomindexmother1:randomindexmother2]
                  j += 1
                  # with this code we take parts of the parents until they will be empty,the problem is that points are going to be repeated
              kid1.append(lpoint)
              for x in kid1:
                  inde = random.uniform(0, len(kid1) - 1)
                  if kid1[inde] in kid1:
                      del kid1[inde]
              distances = []
              totdist = 0
              z = 1
              while z &lt; len(kid1) - 1:
                  # menys 1 ja que tenim len(kid1)-1 intervals
                  distances.append(distpoint(kid1[z - 1], kid1[z]))
                  totdist = sum(distances)
              newpaths.append([kid1, totdist])
              k += 1
          i += 1
      srtlist = sorted(newpaths, key=operator.itemgetter(1))
      return srtlist
  
  x = [[[[3, 4], [2, 4], [5, 8], [2, 4], [4, 5]], 5], [[[3, 4], [6, 8], [4, 6], [6, 4], [4, 5]], 4]]
  Y = generation(x)
  print Y
  </code></pre>