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

9 posts were split to a new topic: How does .pop() work?

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

3 posts were merged into an existing topic: How does .pop() work?

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

2 posts were split to a new topic: Where is the typo in my code?

2 posts were split to a new topic: Should we print inside of the loop?

2 posts were split to a new topic: What is the purpose of index +=1?

2 posts were merged into an existing topic: How does .pop() work?

2 posts were split to a new topic: Why do I get an index error?

7 posts were merged into an existing topic: How does .pop() work?

You are adding students to a Poetry class, the size of which is capped at 6.

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


There might be something I’m not seeing here, but if the class is capped at 6, why isn’t len(students_in_poetry)<=6? I tried it this way and it added a 7th student. I thought len(students_in_poetry)<6 would print 5 elements/students.

We go with less than since when there are 5 elements, the code is allowed to run once more. When we allow =6, then the code runs one more time after that, hence it reaches 7.

Understood. Thank you.

1 Like

Title: Alternative Code (Need Debug Help)
So I decided to write this code not as a while loop just to see if I could. I came up with this:

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

scount = 0

for student in all_students:
    if scount < 6:
        scount += 1

print (students_in_poetry)
print (all_students)
print (scount)

It seems to work just fine up until it scount reaches 5. Then for some reason, it stops increasing and the code just stops.
I’m aware that there are more efficient ways of tackling this problem but I’d like to know what causes this code to stop working at 5?

Removing from the list you are looping isn’t a good idea, its waiting for things to go wrong

the most likely cause is that your loop is done, it has looped over the list (due to all the removed items)