so in the left picture which I try to work out for hours…
it returns True for both example. Whereas the right picture it returns True and False which is right answer.
Can anyone explain how the order of them affect the result please?
It’s beyond my brain capability!!
Thank you!
This worked script for Reversed List:
def reversed_list(lst1, lst2):
lst2 = lst2[::-1] # reversed lst2 … -> 1,2,3
for index in range(len(lst1)):
if lst1[index] != lst2[index]:
return False
return True
This answer is getting me halfway to understanding the problem I’ve been staring at for far too long.
I had tried it as
if lst1[index] == lst2[len(lst2) - 1 - index]:
return True
else:
return False
(i later flipped around the == to != and false/true
In every test or change I made - using a while loop or for loop - it ends the loop after the first iteration/comparison and declared everything True because it never gets to test the second numbers.
So, if we are testing conditions with the if/else statements why do we put the return outside of those tests? (other than that the return stops the loop)
We can perfectly use return False in the loop, when a set of numbers doesn’t equal, we have our answer, so then its fine to end the function “prematurely” using return.
now return True (That the lists are opposites/reversed of each other) is only determined after we looped over all the numbers, which is why we return True after the for loop
You’re carrying out a bunch of actions.
Given what you’ve done so far, is this a good time to quit the function?
This is something you can carry out using pen and paper. So arguably you already know what should happen when.
If you find what you’re looking for, then you’re done, right? And if you don’t, then you’d keep looking.
In particular what I want to point out here is that you already possess the answer, but you’re disconnecting that from your code somehow. Don’t. You have something to refer to, it’s not different, use it.
Thanks for the response. It’s interesting to see how return in different places affects the code. I kept going back and putting print statements to test and see where the code was stopping or continuing. So, i was scratching my head thinking "i know I want it to run through the full loop to test each index but why is it stopping- better yet, how do I get it to NOT stop at the first iteration of the if statement. " This led me to adding an additional loop condition among other failed attempts.
In a later lesson under strings this came up again.
It’s explained pretty well in that instance here Why doesn't `else: return False` work?
AND this link is awesome for helping you literally see the steps the code takes if you were to shift the return indentation.
exiting at different points in time will have different outcomes. this is not surprising! you’re almost suggesting that you can scramble the order of operations and get the same outcome
changing indentation isn’t a difference in text with surprising outcome. did you, or did you not, mean to do something as part of a loop? you’d either put that something in the loop, or not.
some languages use curly braces to mark the beginning and end of loops/functions/whatever
for x in stuff {
// this is inside the loop
}
// this is outside the loop
would it be surprising if you moved something out of or into those braces?
well, there’s no difference whatsoever, those braces are not adding any information, it’s entirely cosmetic
for x in stuff
// this is inside the loop
// this is outside the loop
What I meant there is the opposite - Depending on the placement of indent it has totally different outcomes. As someone learning this and finally understanding that - it is interesting to see it in action. Understanding that gives much better context to the in the loop out of the loop workings.
Just don’t ever “try different indentations”. When you’re writing code you have some specific actions in mind, and you would do that. So it doesn’t depend on your indentation! Your indentation depends on where you mean to put it, it’s the other way around.
Maybe I’m splitting hairs but your wording makes me worry.
I’m abit puzzled over the placement of return True. When i put it as the above, under the loop, it keeps returning True regardless of whether its true. But when I delete the extra spacing and the Return True is under the function, it works perfectly.
It would be great if someone could explain this to me.
Try not to think in what if add or remove whitespace here. But more like: What is the difference in behavior?
So first, what does return do? Return hands back data to called. Signaling that the function is done executing
so when we place return inside the loop, only the first value of the first function parameter (lst1) is compared to the last value of the second function parameter.
When we place return outside/after the loop, there are two possible scenarios:
somewhere during the loop, the values do not match. False is returned.
If false is not returned, the code after the loop is executed, returning true
behavior wise this make sense, we don’t need to compare all values in the lists when the lists aren’t reverse of each other. But only if all values match, we should return True