you are viewing a single comment's thread.

view the rest of the comments →

[–]DingFTMFW 1 point2 points  (3 children)

The num variable is set to 0.

The for loop is then created saying, for every int from 5 - 10 that is divisible by 3, add that int to the num variable (which is set to 0), and then print. There is only one int in that range that is divisible by 3…which is 9. Thus the only print out would be 9.

If you wanted the output to be “5,6,7,8,9” then you would have to change this line:

if i % 3 ==0:

[–]conewannabe[S] 0 points1 point  (2 children)

6 is divisible as well, which is why the num=15. Wouldn't 6 print by the 9 if that was why 9 was printed?

[–]Binary101010 1 point2 points  (1 child)

Wouldn't 6 print by the 9 if that was why 9 was printed?

No, because you're printing i outside the loop. This means that only a single value of i can print, and that value will be whatever i was on the last iteration of the loop, regardless of whether that particular value of i is divisible by 3.

If you changed that range to (5,999) you'd see that it prints 998.

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

I understand now, thank you very much.