Why does my code print none?

I am wondering why the following code:

#Write your function here
def double_index(lst, index):
  new_list = lst
  try:
    new_list[index] = new_list[index] * 2
    print(new_list)
  except IndexError:
    return lst

#Uncomment the line below when your function is done
print(double_index([3, 8, -10, 12], 2))

Gives me this output:

[3, 8, -20, 12]
None

Why does it say “None”? (Making me unable to proceed the lesson)

EDIT: Nevermind! Found it out myself. The print() expression within the function has to be replaced by “return new_list” (without quotes), because the function itself should just return a a list which can be printed out (at the bottom end of the code).

Yes! As always, remember:

  • We print() output that we want to see on the screen.
  • We return output that Python needs to use, e.g., to assign to a variable or to pass to another function.
4 Likes