FAQ: Learn Python: Loops - Infinite Loops

yup - starting to make sense now - thanks ever so much. I’m just afraid of what other important bit of info fell out of my brain to accommodate this one :joy:

1 Like

Hi all.

I´m very new to all of this, so please excuse me if I fail to use the correct term /jargon for things.

Anyway, I managed to get to the end of the exercise and compare it to the default answer. Turns out I got it right, but I don´t understand one thing.

The final print out of the exercise printed only the items from students_period_A, even though throughout the exercise they reference combining both lists. Shouldn´t the final print out be a list of both lists combined? I feel like all I did was replace the items in students_period_B with the items in list students_period_A.

My answer below:

students_period_A = [“Alex”, “Briana”, “Cheri”, “Daniele”]
students_period_B = [“Dora”, “Minerva”, “Alexa”, “Obie”]
for student in students_period_A:
students_period_B.append(student)
print(student)

Kind regards,
Leandro Johann Silva

2 Likes

I have the same train of thought and question.
:grinning:

1 Like

Apparently we´re not getting our responses. Did you manage to figure it out?

Consider what your are looping though here (students_period_A) and what values student would take on each iteration of this loop-

for student in students_period_A:
    students_period_B.append(student)  # we alter the list: students_period_B
    print(student)  # this prints once every iteration
1 Like

I have the same question and don’t really understand the response.

If we are looping each element in students_period_A = [“Alex”, “Briana”, “Cheri”, “Daniele”] , and/or taking each element from students_period_A and adding it to the end of students_period_B I would of expected the code to print the list of names four times adding one name from students_period_A each time?

why does it only print the names in students_period_A ?
Dora
Minerva
Alexa
Obie

1 Like

The question asked in the post I had responded to which you are now replying to seems different from what you are asking. Could you provide the code your question pertains to?

Hi, i have the same question as @gpol. the lesson 4 infinite loops in Loops asking us to combine student list A and B into one using append. I have done the coding as attached. Suppose the output of the coding should include both list, but i only able to get the student list in student list A, not combine with B (check the picture for the output)

. why?

[code](https://students_period_A = [“Alex”, “Briana”, “Cheri”, “Daniele”] students_period_B = [“Dora”, “Minerva”, “Alexa”, “Obie”] for student in students_period_A: #students_period_A.append(student) students_period_B.append(student) print(student))

1 Like
for student in students_period_A:
  students_period_B.append(student)
  print(student)

In your loop, you are iterating over each element in students_period_A. After appending it to the end of students_period_B, you are printing out the value of student.

Each iteration, student is assigned the next value in the list students_period_A. This means, student will have the value of "Alex" the first iteration, "Briana" the second iteration, "Cheri" the third iteration, and so on.

The statement print(student) prints the value of student during that iteration. This means that "Alex" is printed the first iteration, "Briana" the second iteration, and so on.

You mentioned that the output should “include both lists,” what do you mean by this? The instructions state to print only student during each iteration, which is what your code accomplishes.

2 Likes

Oh! I just realized that that is only “student” in the print statement. sorry, this is my mistake.
What i said to print to include both list shall be printing the student list B, not the student.
Thank you for your kind response! It solved my problem!

This lesson had me scratching my head, because i can’t understand why it would repeatedly append itself.

my_favorite_numbers = [4, 8, 15, 16, 42]
 
for number in my_favorite_numbers:
  my_favorite_numbers.append(1)

i would expect the new result to be [4, 8, 15, 16, 42, 1 ], but why does it have to append itself again infinitely instead of printing out the result?
I can’t find the answer anywhere because they’re all talking about ‘while loops’ everywhere else.

I found this lesson unsatisfactory, as far as explanations go.

The issue is that you’re iterating through a list- my_favorite_numbers and constantly appending to that same list within the loop. So your list is constantly growing with each new append so your loop never reaches the end of the list.

1 Like