Python ‘Append Sum’ Code Challenge — list index out of range

Hello here!

I’m running into an issue with the Append Sum Code Challenge for Python: https://www.codecademy.com/paths/computer-science/tracks/cspath-cs-101/modules/cspath-code-challenges/articles/python-code-challenges-lists

I’ve written and run the following code successfully, howeve, when I submit my Answer, I get à `list index out of range` error.

``````#Write your function here
def append_sum(lst):
index = 1
for i in range(3)
lst.append(lst[index] + lst[index + 1])
index += 1
return lst

#Uncomment the line below when your function is done
print(append_sum([1, 1, 2]))
``````

Could someone please tell me what I’ve done wrong?

Hello @frankode08, welcome to the forums! Notice how your `index` variable begins at `1`? So when your loop loops through the range `0-3`, the `index` variable will be incremented three times:

``````iteration 1, index=1
iteration 2, index=2
iteration 3, index=3
index=4 at the end of the loop
``````

Since list indices start at `0`, a three element list will have the indices `0,1,2`. Since the `index` variable will be two, trying to access `lst[index+1]` equates to `lst[3]`, and, since `3` is beyond the list’s indices, it throws the `index out of range` error.

As an aside, I see that you’re hard-coding your `range()` in your for loop. What would happen it the length of the list wasn’t three?

1 Like

Thank you for your response @codeneutrino .

I’m still a little confused by the problem. Per my line of thinking, the length of the list should increase in place as I append more items/values to it. According to the requirements of the problem, we start with a list with three numbers, thus having a maximum index of 2. Then as I loop through the list three times, I add a new value each time, meaning I end with a list with six numbers, and thus a maximum index of 5.

However, at no point in my calculation does the index variable go past 4. To show you what I mean, here are the steps the function goes through:

1st go through the loop:

``````index = 1
lst.append(lst[1] + lst[2])  # the list, if it started with 3 numbers, now has 4
index += 1                          # the index variable is now 2
``````

2nd go through the loop:

``````index = 2
lst.append(lst[2] + lst[3])  # the list now has 5 numbers
index += 1                          # the index variable is now 3
``````

1st step of the loop:

``````index = 3
lst.append(lst[3] + lst[4])  # the list now has 6
index += 1                          # the index variable is now 4
``````

So, based on my walk-through above, the index should never be out of range… And yet I still get the error above, even though the interpreter ran it successfully before I checked my answer.

And to answer your question: You’re right, as is, with the range hard-coded, it’s not really reusable. I believe adding an argument to the function to get the desired range from the user, and passing it to the range function would be make it more reusable.

Here is the problem, and @codeneutrino tries to point this out, you can’t assume you know the length of the list

if we add more function calls with lists of different sizes:

``````#Uncomment the line below when your function is done
print(append_sum([1, 1, 2]))
print(append_sum([2, 4]))
print(append_sum([3, 5, 7, 9]))
``````

we really start to see the problem, the second call will simply crash the code (IndexError) and the third one will not use the correct values

The function should be written in such a way, that it can deal with a list of any size.

2 Likes

Ah… I understand better now. Thank you @stetim94 and @codeneutrino !

1 Like