Global/local indent placement in Loops

Stuck on the indentation placement of the final return statement in question #4 of Python Code Challenges: Loops (Advanced). See link below …

I visually see the difference in output between my incorrect code solution and the correct Codecademy code solution. But I am failing to process in my head why the outputs are this way. This is my incorrect code solution where line 2 and the last line are indented the same amount (see below). Note that this indentation placement is the same as the placement of the correct code solutions in Questions #2 and Questions #3! …

def same_values(lst1, lst2):
–>new_list = #MATCHING INDENT
–>for index in range(len(lst1)):
---->for index in range(len(lst2)):
------>if lst1[index] == lst2[index]:
–>return new_list #MATCHING INDENT

… compared to the output of the correct code solution where it’s line 4 and the last line that have matching indentation levels …

def same_values(lst1, lst2):
–>new_list =
–>for index in range(len(lst1)):
---->for index in range(len(lst2)): #MATCHING INDENT
------>if lst1[index] == lst2[index]:
---->return new_list #MATCHING INDENT

Befuddled here with global/local placement again … (sigh)

In the second one, the return is inside the outer for-loop. That is not the case for the first one.
In the first one, the return is not inside a loop.

You don’t need nested loops to do this.
you could do something like
for index in range( min(len(lst1), len(lst2) ):
so that you don’t need a for-loop inside another for-loop.

Thank you janbazan for your reply. Yes I see that the placement of the return statements are in different places in relation to where the loops starts. But I can’t visualize WHY the output of my incorrect code yields five sets of [0, 2, 3] … yah I know that there are five indices in both lists. I think I need someone to spell-out/walk-me-through the initial four or five loops of my incorrect code.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.