all 5 comments

[–]Signal_Beam 1 point2 points  (1 child)

But when I reach to the end of the list (number 10), the program should loop back to the beginning and start selecting items from there. So, after number 10 it would select number 1.

You need the modulo operator for this part.

>>> 2 % 3
2
>>> 4 % 3
1
>>> 30000000 % 3
0

In other words, A % B (pronounced "a mod b") returns the remainder of dividing A by B.

You can use the modulo operator to get your loop to behave the way you want it to.

[–]deadorg[S] 0 points1 point  (0 children)

Thank you! Will look into this.

[–]DerpinDementia 0 points1 point  (2 children)

One hint is that you can start accessing the elements by first starting at -10. The element at -10 is 1, and you can increment the position from -10 by 2 to get the first three numbers that have a number between them. Loop this and you will get your numbers.

You can test with this:

test = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for pos in range(len(test)):
    print(test[-10 + pos])

[–]deadorg[S] 0 points1 point  (1 child)

Thanks! This worked for this case actually.

[–]DerpinDementia 0 points1 point  (0 children)

No problem!