title: python, loop nesting, functions, if statements
body: <p>This is a somewhat garbled code excerpt. </p>
  
  <p>The context is that it is trying to iterate through a list, building, which should look like [100, 92, 87, etc]. It wants to iterate through each floor of the building and then move that person (by decrementing the current floor) to the next available staircase.</p>
  
  <p>My issue is nesting list and if statements within functions. It's a triple whammy and I'm having trouble understanding the proper syntax for:</p>
  
  <ul>
  <li><p>calling on specific parts of lists of lists - i.e. list[i][j]</p></li>
  <li><p>using if statements in loops</p></li>
  <li><p>nesting loops</p></li>
  <li><p>using loop variables in other loops</p></li>
  </ul>
  
  <p>Here is my code:</p>
  
  <pre><code>def Evacuate(building):
      while sum(building) &gt; 0:
          for i in building:
              if building[i] &gt; 0:
                  for j in range(STAIRCASE):
                      if staircasenetwork[j[i-1]] &lt; CAPACITY:
                          building[i] -= 1
                          staircase[i-1] += [TRAVELTIME]
  </code></pre>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p>I figured out the issue. Building's input is a list. Staircasenetwork is also a list. The lists look like this</p>
  
  <pre><code>building = [100,90,101]
  staircasenetwork = [[0,0,0],[0,0,0]]
  </code></pre>
  
  <p>This represents a 3 story building full of people and two empty staircases.</p>
  
  <p>What I did wrong was I was trying to write something along the lines of:</p>
  
  <pre><code>for i in building:
      #I'm skipping or simplifying some specific conditionals for the problem here
      for j in staircasenetwork:
          building[i] -= 1
          staircasenetwork[j][i] += 1
  </code></pre>
  
  <p>Which was supposed to take (i) in building (which I expected to be [0, 1, 2]) and (j) in staircasenetwork (which I expected to be [0, 1]) and use those two co-ordinates to iterate through the lists I wrote.</p>
  
  <p>What I forgot was that python iterates through a list by directly assigning those values to (i).</p>
  
  <p>So if I have a list:</p>
  
  <pre><code>[100, 90, 101]
  </code></pre>
  
  <p>And I write:</p>
  
  <pre><code>for i in list:
      print i
  </code></pre>
  
  <p>It will print:</p>
  
  <pre><code>100
  90
  101
  </code></pre>
  
  <p>Not:</p>
  
  <pre><code>0
  1
  2
  </code></pre>
  
  <p>So the fix to my problem was using range(len(building) instead of building. </p>
  
  <p>This statement takes building and first converts it into an integer equal to the length of the list building using len(). Then it takes that integer and converts it into a list of numbers from 0 to X, using range(). </p>
  
  <p>Essentially: [100, 90, 101] >> 3 >> [0, 1, 2]</p>
  
  <pre><code>building = [100, 90, 101]
  for i in range(len(building)):
      print str(range(len(building))) + " " str(building)
  </code></pre>
  
  <p>Will print:</p>
  
  <pre><code>0 100
  1 90
  2 101
  </code></pre>
  
  <p>So in my first attempt, when i use: </p>
  
  <pre><code>for i in building:
      #to call on
      building[i]
  </code></pre>
  
  <p>The first thing it does is it looks at the first value in the building list, which is 100. Then it uses that number to find the 100th number in the building list, which doesn't exist because that list is only 3 values long.</p>
  
  <p>It tries to do this:</p>
  
  <pre><code>building = [100, 90, 101]
  for i in building
  &gt;&gt;&gt; first val of building = 100
      building[100]
      &gt;&gt;&gt; IndexError: list index out of range
      &gt;&gt;&gt; Is looking for the 100th number in a 3-number long list
  </code></pre>
  
  <p>There are ways of using statements like:</p>
  
  <pre><code>for i, e in enumerate(building):
  </code></pre>
  
  <p>To create a list of tuples that may look like:</p>
  
  <pre><code>[(0, 100), (1, 90), (2, 101)]
  </code></pre>
  
  <p>But I preferred to use the range(len(building)) method. This method may not meet great benchmarks for speed, but it succeeds in teaching me something about how code works and it solves my little problem.</p>
  
  <p>The fixed code will read:</p>
  
  <pre><code>for i in range(len(building)):
      for j in range(len(staircasenetwork)):
          building[i] -= 1
          staircasenetwork[j][i] += 1
  </code></pre>
  
  <p>Now (i) and (j) will be the co-ordinates of (via range(len())) rather than direct reflections of the variables.</p>
  
  <p>Lego Stormtrooper has also written a great response to other issues with my code.</p>