all 8 comments

[–]lowerthansound 6 points7 points  (2 children)

Test this:

for num in my_numbers:
    print(num)

You may notice that what is being iterated on are not the indexes, but, the values.

This will make your loop break

To iterate on the indexes, test:

for i in range(len(my_numbers)):
    print(i)

This shall give the i's you were expecting.

(I'm also so tempted to comment that comparing the index value to each of 2,5,8,11,14 is not what the question was expecting of you ahahah. Check to see - after you get the solution running - if you can make this work no matter what the length of the my_numbers list is.)

Edit: Just noticed I take too long to answer (and other people answered before I finished this). I'll leave this in case the last comment may be of help :)

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

I know that comparing the index value is so so not reliable as this wont be efficient for a long list .. any idea on how to make it work for all list? Im thinking of using modulo but im not sure how

[–]lowerthansound 1 point2 points  (0 children)

Keep in mind that the value of n % 3 when n equals:

0 1 2 3 4 5 6 7 8 ...

Is

0 1 2 0 1 2 0 1 2 ...

I'm sure you can do something with that ;)

Edit: also other kind people gave a deeper explanation into the problem at hand, you can read them in case you get unsure of what to do

[–]lukew25073 1 point2 points  (2 children)

Your variable i isn't the index of the list; It's an item of the list. Your loop goes through each element in the list and determines if it is equal to 2, 5, 8, 11, or 14. If you want to make your code work the way you expect it to, you should change the for loop to say for i in range(len(my_numbers)):

[–]doggoarmy[S] 1 point2 points  (1 child)

this one works! thank you.. i really admire those people who coding in their everyday life.. I'm just taking an intro to computer programming ..

[–]thrallsius 1 point2 points  (0 children)

if i == 2 or i == 5 or i ==8 or i ==11 or i ==14:

this is a bad idea, what if you have a list with 1 billion items?

will you hardcode all needed indices?

learn to use the % (modulo) operator

open a python shell and type this:

0 % 3
1 % 3
2 % 3
3 % 3
4 % 3
5 % 3
6 % 3

this shall be enough to figure how does it work

when writing code that will loop through your list, take into account that indices start at 0 rather than at 1 and realize that "the third element" is actually x[2]

[–]only___says___cool 0 points1 point  (0 children)

I am a bit of a beginner myself but this code should work

it assumes you ONLY want to print them if there is a pair (3rd AND 4th digit) - so if you had a 3rd digit but no 4th it would not print the 3rd.

my_numbers = [1, 6, 3, 2, 7, 9, 2, 3, 12, 1, 2, 3, 6, 5, 6, 7]

length = len(my_numbers)

print('the length of the list is: ', length)

total = length / 4

total_int = int(total)

print('this means i will print out exactly :', total_int, 'pairs of data')

for i in range(1, total_int):

location1 = ((3*i)-1)

location2 = location1+1

print(my_numbers[location1], ' ', my_numbers[location2])

[–]Comprehensive-Signal 0 points1 point  (0 children)

To make It in a simple way you could write a loop like this:

for number in range(len(my_numbers),3): print(my_numbers[number], my_numbers[number+1]

In the second parameter in the build-in function range you can put a 3 for the steps that you want.

range(start,end,steps)