Jump to content

I am new to programming and was wondering what was wrong with the companions pop() line in the Ep...


G+_Justin Eiesland
 Share

Recommended Posts

To further expand on what Lee Crocker said, it appears that Python internally is keeping a counter going when you loop over a list.  So the first time through, it picks the first item in the list, the second time it gets the 2nd item, the 3rd time through selects the 3rd item, and so on.  In fact, it gets the current 2nd or 3rd item, even if the list has changed.  So, the first time time through, the companions list has three items, and then we do a pop().  Now the 2nd time through the loop, there are only two items in the list.  If they would have printed out the current item in the for loop, you would have seen that it was the current 2nd item, which is now the last item in the list.  So then the pop() was done the 2nd time, now there is only one item left in the list.  The 3rd time through the loop Python is actually looking for the 3rd item, but there is none.  That's why it stops.

Link to comment
Share on other sites

 Share

×
×
  • Create New...