Skip to content
Advertisement

Python Loops result not understanding [closed]

Can anyone please help explain what the hell is going on with this code and why it is printing ‘Hello Hi’ two times every time?Questionable Code

Advertisement

Answer

The submitted code is doing 2 things. First, in :

JavaScript

The code is iterating through (0,1,2,3), and printing its current value. This seems to be intended.

Then, within this loop, in:

JavaScript

The code is iterating through the elements of y, which is [“Hello”, “Hi”], and printing y back each time. So the loop iterates twice, and each time it prints out all of y, so it prints [“Hello”, “Hi”].

Your question leads me to believe that you are trying to produce code that prints the elements of y seperately, and so something like this may be what you are looking for:

JavaScript

This will output:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement