FAQ: Learn Python: Loops - While Loops


#1

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!


#2

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:
students_in_poetry.append(all_students[index])
if len(students_in_poetry) < 6:
index += -1
else:
break
print(students_in_poetry)


#3

Where does the .pop() statement go?


#5

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()

#6

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()
print(students_in_poetry)
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 ???

Thx


#7

Wow, I get it. I was close…


#8

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.

best,

J


#9

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?


#11

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


#12

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()
students_in_poetry.append(student)

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?


split this topic #14

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


#17

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()
  students_in_poetry.append(student)

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.


#18

One more way to go about it:

while len(students_in_poetry) < 6:
  students_in_poetry.append(all_students.pop())

print(students_in_poetry)

#19

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:
  students_in_poetry.append(all_students[-1])
  all_students.pop()
print(students_in_poetry)

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.


#20

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()
#создаем и приравниваем переменную студент к функции все_студенты
#если это не сделать, на следующем шаге функция не определится
students_in_poetry.append(student)
#здесь добавляем созданную переменную в функцию