FAQ: Learn Python: Loops - While Loops

This community-built FAQ covers the “While Loops” exercise from the lesson “Learn Python: Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise While Loops

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1 Like

Hey, guys. That loop syntax is very hard to me. I really didn’t understood how to get items from that list with .pop, so I created a solution using .append to match the answer:

index = -1

while len(students_in_poetry) < 6:
if len(students_in_poetry) < 6:
index += -1


Where does the .pop() statement go?

1 Like

If you read the post you are replying to you will see that the member doesn’t know how to use .pop(), either.

The list.pop() method can be used in one of two ways.

  1. Last on, First off. This means it will always take the element at the end of the list.
  2. Selectively, which means at any valid index.
item = items.pop()

item will be the last value in the items list.

item = items.pop(index)

item will be the value at items[index].

We want the six students at the end of the all_students list.

student = all_students.pop()

Hi Roy,

I tried that:

all_students = [“Alex”, “Briana”, “Cheri”, “Daniele”, “Dora”, “Minerva”, “Alexa”, “Obie”, “Arius”, “Loki”]
students_in_poetry =

index = 0
while len(students_in_poetry) < 6:
students_in_poetry = all_students.pop()
index += 1

but I have this answer

Value for students_in_poetry did not match ['Loki', 'Arius', 'Obie', 'Alexa', 'Minerva', 'Dora'] , (was Minerva ) ???

I tried many value to replace < 6 but it doesn’t work .
Something else, I don’t understand is why to use .append (in get stuck), we cannot use only .pop ???


1 Like

Wow, I get it. I was close…

1 Like

While not telling me your solution, what was the idea that changed your function? I have tried many different variations, including something similar to what you wrote but I cannot seem to get the correct function.



There are two things to keep in mind about .pop().

  1. What we pop can be assigned to a variable, or used in an expression.
  2. It shortens the list by one element.

Since we are using while, then what will the condition be set at? What are we to do with the popped element?


Having the same issue. This explanation/exercise is very poorly worded.


The lesson text is brief, but it does explain perfectly how while works. By now we should be well familiar with conditional expressions.

while condition:
    # do this

When the condition is not met, the loop ceases.

While the length of the students_in_poetry list is less than 6 , use .pop() to take a student off the all_students list and add it to the students_in_poetry list.

students_in_poetry = []
while len(students_in_poetry) < 6:
    # do this

Since the length of students_in_poetry is 0, the loop body code will execute.

student = all_students.pop()

Notice that the students_in_poetry list is growing as we append each student, which makes sense. Once that list reaches 6 in length, the loop ceases.
There is a caution that we should be aware of, something that was brought up earlier… infinite loops. Can we predict what would cause the above loop to go on forever?


A post was split to a new topic: Any other way to solve the same problem

Great discussion, I want to add how I viewed it. Building onto the hint explanation the full answer is

while len(students_in_poetry) < 6:
  student = all_students.pop()

We want to learn how we arrived at this.

Firstly, the task is to fill up poetry class students as long as the number of students falls below 6.
Next, we want to remove the students from the pool of “total students” to avoid double counting [as we plan to add them to poetry class].
Lastly, after removing the students from the pool of “total students” we add them through .append to students_in_poetry.


One more way to go about it:

while len(students_in_poetry) < 6:


I solved it fairly similar:

all_students = ["Alex", "Briana", "Cheri", "Daniele", "Dora", "Minerva", "Alexa", "Obie", "Arius", "Loki"]
students_in_poetry = []

while len(students_in_poetry) < 6:

So I append the last student on the list, then remove them until the poetry class reaches a limit of 6 students.
Hope that it can inspire.


Personally, i want to say, it’s not the first poorly written and broken exercise in this course.
It takes a 1,5 hour from me to understand how and why it works this way.

Below is step-by-step description:
while len(students_in_poetry) < 6:
#пока длина (len) студентов_в_поэзии меньше 6:
student = all_students.pop()
#создаем и приравниваем переменную студент к функции все_студенты
#если это не сделать, на следующем шаге функция не определится
#здесь добавляем созданную переменную в функцию

1 Like

Hi All

I read the thread but I am still getting an error

> all_students = ["Alex", "Briana", "Cheri", "Daniele", "Dora", "Minerva", "Alexa", "Obie", "Arius", "Loki"]
> students_in_poetry = []
> while len(students_in_poetry) < 6:
>   name = all_students.pop()
>   students_in_poetry.append(name)
> print (students_in_poetry)
> output : ['Loki', 'Arius', 'Obie', 'Alexa', 'Minerva', 'Dora']
> Error:
> Expected the `students_in_poetry` list to be printed

I have tried various coding statements and they give the output to satisfy the first part of the exercise, but I am not able to get past the second part!!
What am i doing wrong?

Nothing we can see. However, the SCT may not be expecting whitespace.

print (students_in_poetry)

Try removing that space.

Why does print have to be outside the while loop?

Because that is the intended last step of the program. It may be useful to print inside the loop to inspect each iteration’s effect on the list, but the final printing pretty well proves such inspection unnecessary, and all we want to see printed is the final result.

In the example given for While Loops—

dog_breeds = [‘bulldog’, ‘dalmation’, ‘shihtzu’, ‘poodle’, ‘collie’]

index = 0
while index < len(dog_breeds):
index += 1

What is the purpose of—

index += 1