Https://www.codecademy.com/courses/learn-python/lessons/lists-and-functions/exercises/modifying-each-element-in-a-list-in-a-function?action=lesson_resume


#1

Why does the return indent spacing determine the value displayed on the console?
Here is what happened:

n = [3, 5, 7]

def double_list(x):  #returned to console: n = [6,5,7]
  for i in range(0, len(x)):
    x[i] = x[i] * 2
    return x


print double_list(n)
n = [3, 5, 7]

def double_list(x):  #returned to console: n = [6,10,14]
  for i in range(0, len(x)):
    x[i] = x[i] * 2
  return x

print double_list(n)

I don’t quite understand how an indent can make such differing results?
Any explanations?


#2

There are plenty of topics explaining on this matter. I should really store this in a file, and then copy paste this every time this question comes up. (sometimes multiple times a week)

By default, a function returns None at the end of the function. If we want to return something else at the end of the function, we can use the return keyword.

Given return is the last thing a function does, when a return keyword is reached, the function ends


#3

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