# Advanced Python code challenges--loops

Ok so below are two different versions of my solution to the 5th problem on this page " Reversed Loops "

i get THAT the first one doesnt really work… and i FEEL there is a why… but im struggling to SEE it… can someone EXPLAIN what is happening INSIDE the IF/ELSE version of my logic that prevents from getting the right answer under False circumstances

Non Functional Code Block

``````#Write your function here
def reversed_list(lst1 , lst2):
new_list = []
for index in range(len(lst1)):
if lst1[index] != lst2[len(lst2) -1- index]:
return False
else:
return True

#Uncomment the lines below when your function is done
print(reversed_list([1, 2, 3], [3, 2, 1]))
print(reversed_list([1, 5, 3], [3, 2, 1]))
``````

Functional Code Block

``````#Write your function here
def reversed_list(lst1 , lst2):
new_list = []
for index in range(len(lst1)):
if lst1[index] != lst2[len(lst2) -1- index]:
return False
return True

#Uncomment the lines below when your function is done
print(reversed_list([1, 2, 3], [3, 2, 1]))
print(reversed_list([1, 5, 3], [3, 2, 1]))
``````

unless its because. …
a return ends a function?
so while the else statement remained in the for loop and if any number of the first elements being the same… it spits out True and ended the loop???

if this is why, please just yes this comment or something? Cuz boyy even though i solved it… im just staring at it

A `return` immediately exits the function.

In the first version, you will run only one iteration of the loop and will return either `True` or `False` without iterating over the remaining elements.

In the second version, if the condition is met, then you will immediately return `False` and will exit the function. If you finish all the iterations and exit the loop without the condition having been met even once, then you return `True`.

1 Like