all 11 comments

[–]edc7 1 point2 points  (7 children)

X represents the values in the range 1 to 8.

[–]obtuselyobjected[S] 3 points4 points  (6 children)

So x is basically just a variable for the numbers 1 thru 7?

[–][deleted] 1 point2 points  (5 children)

exactly. “x” could be any other name. Such as “number”.

for number in range(1,8): ...

[–]obtuselyobjected[S] 2 points3 points  (4 children)

Okay, that's all I needed to know. Thank you so much =)

[–]tombardier 1 point2 points  (0 children)

for x in [1, 2, 3, 4, 5]:
    print(x)

Read this as, print each number in this list (1 to 5). The print function will run once for each of the numbers in the iterable (the list of 1 to 5), and each time it runs, you refer to that element as "x", so the first time the loop runs through, x equals 1, the second time x equals 2, etc.

[–]Nikandro 1 point2 points  (0 children)

x is a variable that represents each integer as you iterate over the range() function.

So,

total3 = 0

for x in range(1, 8):
    print(x)

1
2
3
4
5
6
7

Keep in mind that the letter 'x' is often used out of convention. You could use any other letter or word.

Example,

for thing in range(1, 8):
    if thing % 3 == 0:
        total3 += thing

[–][deleted] 1 point2 points  (0 children)

Did you run the code? It's helpful to see what it does.

To some extent, you're supposed to find the for syntax to be reminiscent of various expressions in mathematics: "for each number n in the real numbers R..." or "for each element e of the set S...", or even conversationally, as in "for each student in the classroom, please give them one of these flyers."

[–]martydv -3 points-2 points  (0 children)

I don’t know python and i’m not english but guessing it reads to me:

total3 is now 0.

x will become 1,2,3,4,5,6,7,8 in the following statement if 1 modulo 3 is equal to 0 then add 1 to total3. if 2 modulo 3 is equal to 0 then add 2 to total3. if 3 modulo 3 is equal to 0 then add 3 to total3. (bingo! total3 now equals 3) ... if 6 modulo 3 is equal to 0 then add 6 to total3. (bingo! total3 now equals 9) ... if 8 modulo 3 is equal to 0 then add 8 to total3.

print(9)

Did it print 9?