Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Dorota Parzych
Dorota Parzych
5,706 Points

list index out of range and how to do a task

Hi, I am in the middle of writing my code and I don't know why it says that the list index is out of range... if I add one to my index in first iteration(and in the beginning it is equal zero) then it should be equal one which clearly can't be out of range... (I want to write a function that: accepts a 6 element list entered by the user Checks if adjacent items have the same value If condition 2 is met, it sums them up together. If the condition does not apply, it squares the third element. It indicates which number appeared the most times in the list and gives the number of its occurrences) Example: Input: [3,2,3,3,3,3] Output: 12 Number most frequently appearing in the list: 3 Number of occurrences of the most frequently appearing in the list: 5 Ps. I wanted to put it in a function later...

#receiving a list from the user
lst = []
amount = 0
for i in range(0, 6): 
    ele = int(input()) 
    lst.append(ele)
#checking if the adjacent numbers are equal     
    while(lst[i]==lst[i+1]):
      amount += lst[i]
    else:
      amount = lst[2]*lst[2]


print(lst) 

2 Answers

Steven Parker
Steven Parker
229,582 Points

The loop adds a single element to the list in each pass, (index "i"), but the "while" code accesses both lst[i] and lst[i+1]. So for every pass, lst[i+1] is attempting to access an element that does not yet exist, and is therefore "out of range".

Dorota Parzych
Dorota Parzych
5,706 Points

So how should I do it then?

Steven Parker
Steven Parker
229,582 Points

An alternative strategy might be to compare against the previous item, but only when the current index is higher than 0.

Dorota Parzych
Dorota Parzych
5,706 Points

So should I make a temporary variable that will store the previous number and compare it?

Steven Parker
Steven Parker
229,582 Points

You don't need a separate variable, since the previous value will be lst[i-1] (for every value of "i" other than 0).

Sean M
Sean M
7,344 Points

What are you trying to do with your code?

Dorota Parzych
Dorota Parzych
5,706 Points

I want to write a function that: accepts a 6 element list entered by the user Checks if adjacent items have the same value If condition 2 is met, it sums them up together. If the condition does not apply, it squares the third element. It indicates which number appeared the most times in the list and gives the number of its occurrences) Example: Input: [3,2,3,3,3,3] Output: 12 Number most frequently appearing in the list: 3 Number of occurrences of the most frequently appearing in the list: 5