Problem with my computer or with the code? Unsure


This might not be related to the exercise? But it might be?

I am running this code:

students_period_A = [“Alex”, “Briana”, “Cheri”, “Daniele”]
students_period_B = [“Dora”, “Minerva”, “Alexa”, “Obie”]
for students in students_period_B:

And when I hit the button “Run”, the code actually never runs and stays in that loading circle symbol. It has never happened to me so not sure if the code is wrong or something wrong with my pc? I have tried in a different browser but I get the same issue.

UPDATE: This last time I left it waiting for a long time, it eventually threw this message "Failed to test your code."

Hello @frankjimenez93270473, this is because you are creating an infinite loop, When you use a for loop, you have to be careful when mutating the object you’re iterating over:

a = ["a"]
b = ["b"]
for i in a:

You see here, you can never reach the end. This is because the code does the following:
for every item in a (“a”), do this:
append b (“b”) to a.
Now the list a is ["a", "b"]. So the code continues.
For every item in a (“a” and “b”):
append “b” to the end. Now you get:
["a", "b", "b", "b"].
If you want to append one list to another, you can do this:

a = ["a"]
b = ["a", "b"]

The list a is now: ["a", ["a", "b"]].
If you wanted to have ["a", "b", "c"], you could do something like this:

a = ["a"]
b = ["a"]
c = ["a", "b"]
for i in b:
  for x in c:

This way, you do not mutate the list you are iterating through, but since the list you are iterating over is the same as the original list, you get the same results without an infinite-loop.
The above code will print:

>>["a", "a", "b"]

I hope this helps!

Ahhhhhh! That totally makes sense! Sorry. I was so caught up thinking it was something to do with my internet connection that forgotten about the code.

Thank you so much for the careful explanation :slight_smile:

1 Like