Codecademy Forums

FAQ: Learn Python: Loops - While Loops

We need to be sure that i is constantly changing (increasing) so that the test condition eventually fails, otherwise the loop will run indefinitely (infinite loop). We wouldn’t want that.

Can anyone explain why the code says < 6 for poetry students? Capped at 6 is supposed to mean =< 6 (if I’m not wrong). Also the use of .pop is not explained prior to this exercise so had to guess

The loop will keep iterating until that condition fails, which means there will be exactly 6 members in the capped list.

Not sure that pop has not been covered yet. It should have come up in the unit on lists and or dictionaries, which both have a pop method.

list.pop(index)

pops from anywhere in the list;

list.pop()

pops the last item from the list.

Hello everyone!

please explain why this code below makes an ‘IndexError: pop from empty list’ ? thanks in advance

all_students = [“Alex”, “Briana”, “Cheri”, “Daniele”, “Dora”, “Minerva”, “Alexa”, “Obie”, “Arius”, “Loki”]
students_in_poetry =
i = len(students_in_poetry)
while i < 6:
student = all_students.pop()
students_in_poetry.append(student)

print(students_in_poetry)

maybe insert print statement to see the problem?

purely looking at the code, it seems you have an infinity loop (the loop condition will never be false), thus it will pop all the students from all_students, so then the list is empty, and it will still attempt a pop, which results in the IndexError you described