Codecademy Forums

FAQ: Learn Python: Loops - Infinite Loops

This community-built FAQ covers the “Infinite 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 Infinite 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!

I’m a little confused about this exercise. When I put this code in:
for students in students_period_A:
students_period_B.append(students_period_A)
print(students)
I get this output:
Alex
Briana
Cheri
Daniele
My question is why is list B (student_period_B) not also showing if I am adding student_period_A to student_period_B?
My second question is why doesn’t the code work if I have student_period_A.append(student_period_B)?
Thanks in advance

In
for students in students_period_A:
students_period_B.append(students_period_A)
print(students)

The temporary variable students is created and it stores the contents of list A that are selected.
If you print the students variable you will print the contents of list A that are selected.

As for your second question will have to see the code. It should work so it could just be a spelling mistake or a syntax error.

# I want to make it clear I will be using very little python verbage to explain this in plain english.

students_period_A = ["Alex", "Briana", "Cheri", "Daniele"]
students_period_B = ["Dora", "Minerva", "Alexa", "Obie"]

for student in students_period_A:
  # student is the temporary variable.  This is what we are going to call in each list.
  # You want to put A into B.  So "A" is going to be the focus for the statement.
  -----------.append()
  # So now, each students_period_A is now counted as "students".  So you want to........ 
  # If you print it you should see only four people, as it is only wanting to print the students, not the variable
  print(student)
1 Like
for student in students_period_A:
  students_period_B.append(student)
  print(student)
2 Likes

Try to ‘print(students_period_B)’ at the end of the final code, you’ll see List A has been added to List B.

2 Likes

When I try to write an infinite loop, the program doesn’t run. Fortunately I set it back to the standard loop of 4 and it let me continue for some reason.

1 Like

In your comment “# You want to put A into B.” and then at the last comment it says “If you print it you should see only four people,…” then what is the point of even writing “--------.append()” code? if you can not add elements of B in A. It was not clear for me to understand the purpose of -----------.append()'s existence.

Yes. I discovered .append() while trying to solve this exercise as well! :slight_smile:

Okay just a heads up, don’t actually do step 3. I ran the infinite loop just to test it like it told me too and this lesson is actually broken for me. I can go back a step, and access other lessons, but I literally can’t continue past this step no matter how many times I sign out and in, refresh, etc. Gonna try clearing cookies now.

2 Likes

I actually can’t continue this exercise after running the infinite loop. What the heck do I do? I’ve refreshed, cleared cookies, signed out and back in, reset progress, everything. It will not let me continue this exercise

3 Likes

for student in students_period_A:
students_period_B.append(student)

for student in students_period_A:
students_period_A.append(student)

Can you please explain to me why the 2nd one creates an infinite loop and the first one no?

Yet again, another one completely broken exercise.
Initial conditions askus to append students from one list to other with temporary full list as income.
To do this, we should make this:
for student in students_period_B:
students_period_A.append(student)
print(student)

instead the:

for students in students_period_A:
students_period_B.append(students)
print(students)

In both statements the loop should last for as long as there are a number of students in the students_period_A list, but in the second statement, you’re adding a new student to students_period_A in each iteration.

So, basically the loop is trying to finish, but the the goal keeps getting moved, adding one more student to the list on each iteration. It’ll never complete and becomes infinite.

At least, that’s my understanding. Open to corrections/clarifications.

Thanks! Something I found very helpful was to use something arbitrary for the temporary variable. For example, in this solution, you can replace ‘student’ with ‘i’. You can’t complete the exercise that way, but it jumps out at you what ‘i’ is doing:

for i in students_period_A:
  students_period_B.append(i)
  print(i)```

I’m glad you pointed out that the iteration variable can have any name. It just exists to be assigned, one after the other, the items in the iterable which follows the operator in.

As time goes on, you will find that it is quite common (and, IMHO, sensible) to name the iterable with a plural noun, and the iteration variable with the singular of the same noun (or some variation thereof).

I had the same problem! I was able to move on (after Firefox crashed, and a few refreshes) when I change the iteration variable. I.e., at first I had for s in students_period_A: ... then I changed all the instances of s to a and it could run and continue to the next step. Hope this helps!