While Loops - spoiler

Hello!

I am on the while loops section and I must say I am very confused by the lesson text. I don’t understand those two lines from the lesson text’s code about index. The exercise itself doesn’t end up using the index lines. The intro:

dog_breeds = ['bulldog', 'dalmation', 'shihtzu', 'poodle', 'collie']

index = 0                                               # ? why is this here ???
while index < len(dog_breeds):
  print(dog_breeds[index])
  index += 1                                      # I think I know what this does,I think, but there is no explanation.

I was confused and thought we needed to use something like this in the code. The solution makes total sense, but I still don’t understand the lesson instructions themselves. Could you help me? Many thanks in advance!!!

Can you then also post the exercise url so we can actually see the instructions

kind of confusing to use a while loop here, a for loop would be a better fit.

You need to keep track of where in the list you are at, for this we use a variable named index. Which we assign a value of zero. We choice zero, because this is the value of the first index in the list

then each iteration of the loop, we increase index to move to the next element in the list

Oh sorry, here: https://www.codecademy.com/courses/learn-python-3/lessons/learn-python-loops/exercises/while

(Yes, I think what you are saying about the for loop being a better fit is what confused me. I don’t know how and why I would use it in the exercise.)

I would use it? Do you mean for loop or while loop?

Maybe done on purpose by the exercise? To introduce a new concept yet doing some familiar (looping over a list) to minimize new things? Could be

While loop generally makes more sense for something you don’t know the amount of iteration of, like keep prompting the user for input until they enter something valid.

2 Likes

So, what I mean is that the intro text introduces this concept that we’ve not seen yet, the combo of index = 0 and then index += 1, doesn’t explain it (sure, maybe on purpose), and then in the assignment we don’t even need it (we just use len). And I can’t move on because this index syntax thing is new and I want to understand it :). I am also not yet so sure what’s it useful for as we are not using it in the exercise. When I run it in terminal, it just prints the list, just as if we used a for loop.

I am sorry, I am probably not explaining my confusion well. But in the grand scheme of things it’s probably not so important atm. Thanks for trying to help! :slight_smile:

you mean the while loop/condition? index is certainly used:

while index < len(dog_breeds):

I see index

When using a for loop, the loop keeps track of where we are in the list (while looping over the list).

When using a while loop, we have to do manually keep track where we are in the loop using a variable. We increase the variable every iteration of the loop, until the while condition becomes false.

Oh sorry, I mean in the actual assignment that comes after, about the students. I can’t post the solution in here.

When using a for loop, the loop keeps track of where we are in the list (while looping over the list).

When using a while loop, we have to do manually keep track where we are in the loop using a variable. We increase the variable every iteration of the loop, until the while condition becomes false.

=> Thank you, I think I understand this better now. But in the exercise after, the variable is being increased automatically right? Without us having to add it. (I hope you can see the solution to the ‘students’ exercise.

This line of code increments the variable index by 1:

index += 1 

Hi there! Sorry not the code I am talking about, I mean in the solution. I posted the solution here but it got removed. I’ll try again (editing the title of this post to show a spoiler). Here:

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

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

Hello there!

There is no variable that is being increased automatically here (the students exercise). Instead, because .append() adds elements to the students_in_poetry list, a while loop is used. This loop will keep repeating while students_in_poetry has less than 6 elements. Once it has 6 or more elements, the loop will stop iterating.

This mean that a maximum of 6 students can be put in the students_in_poetry list, as the condition of the while loop is evaluated at the beginning of the loop.

Hi! Right, that makes sense, thanks!

I am still confused about the index = 0 and index += 1, but I will google some examples and try to understand its use.

No problem!

Scratch that @kattkaa. I meant to say that, while index = 0 and index += 1 are used for while loops, a for loop would be more appropriate.

For example:

for i in dog_breeds:
  print(i)

i here is assigned each element in dog_breeds each time the loop iterates.

Ah, so I wasn’t totally silly when I found it confusing here phew :slight_smile: