Why exactly in this example is it giving me "NoneType object is not subscriptable"?



in the end I looked for the solution to this exercise but am however still curious to know why exactly my code was given “NoneType object is not subscriptable”, that way I hopefully don’t repeat the same error. So this was my code:

def append_sum(lst):
  sum1 = lst[-1] + lst[-2]
  lst = lst.append(sum1)
  sum2 = lst[-1] + lst[-2]
  lst = lst.append(sum2)
  sum3 = lst[-1] + lst[-2]
  lst = lst.append(sum3)
  return list

Thank you.

What does your call to the function look like?

1 Like

This is what is written at the end of the exercise:

print(append_sum([1, 1, 2]))

I apologize if I’m not giving you the answer you’re looking for.

That line isn’t still in the code, is it?

1 Like

Hi @ai-2090,

The NoneType object, namely None, is what is assigned to lst in this statement:

  lst = lst.append(sum1)

Then your code attempts to subscript lst here:

  sum2 = lst[-1] + lst[-2]

See the following:


Thank you @mtf and @appylpye for your help, I think I’m getting it now, thanks also to a reply I got on my other topic concerning NoneType. Later in the day I’ll go as well through the link appylpye provided.

Have a nice day.


Nice strategy you used about typeerror nonetype object is not subscriptable