Python loop challenges

URL is provided below

I wonder why returning value i returns only one single value when a loop code is written.
It seems in order to produce several values a list must be made and appended to it. But I do not know the difference. The difference between making an empty list and not making one.

https://www.codecademy.com/paths/computer-science/tracks/cspath-flow-data-iteration/modules/dspath-python-loops/lessons/python-functions-loops-cc/exercises/same-index

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

How did you use return? The exercise url is useful, but it will load the users own code. Please copy paste your code to the forum

if you used return in the loop, then it makes sense you get a single value. return means handing back data to the caller and signaling the function is done executing.

1 Like
def same_values(lst1, lst2):
  same_indices_lst = []
  i = 0
  for i in range(len(lst1)-1):
    if lst1[i] == lst2[i]:
      return i

also a similar question is below. I wish to go through the whole loop without quitting in the middle. Then how can I return any value?

def reversed_list(lst1, lst2):
  for i in range(len(lst1)):
    if lst1[i] == lst2[len(lst2)-1-i]:
      return True
    elif lst1[i] != lst2[len(lst2)-1-i]:
      return False  

Hope this information is enough

so my predictions where correct:

I got this running correctly. I wonder if this is a conventional way or if there is any other way.

def reversed_list(lst1, lst2):
  for i in range(len(lst1)):
    if lst1[i] != lst2[len(lst2)-1-i]:
      return False
  return True
1 Like