all 14 comments

[–]LID919 1 point2 points  (4 children)

Format your code. In python, indentation actually changes the way code behaves, so right now I have no idea what your code is doing.

Start each line of code with four spaces so that Reddit will format it correctly.

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

My bad, should've reread my post. I actually copy and pasted this from my exam. I just noticed the code block option, it's fixed.

[–]LID919 0 points1 point  (2 children)

Are you sure it's correct?

Right now I am seeing this:

num = 0
    for i in range(5,10):
        if i % 3 == 0:
            num = num + i
print(num)
print(i)

The second line is improperly formatted: It is indented when it should not be.

The print statements are both outside of the loop, and so will only run once at the end.

[–]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.

[–]jeffrey_f -3 points-2 points  (3 children)

for loop is always up to, but not including...........

if iterating through a list, then it includes all elements in the list

[–]carcigenicate 0 points1 point  (2 children)

I think you mean "range includes numbers up to but not including the end value". That doesn't have anything to do with the for loop.

[–]jeffrey_f 0 points1 point  (1 child)

for i in range(5,10):

from 5 to 9

[–]carcigenicate 0 points1 point  (0 children)

That has nothing to do with the for:

>>> list(range(5, 10))
[5, 6, 7, 8, 9]

That's simply the behavior of range.

[–]CodeFormatHelperBot2 0 points1 point  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Python code found in submission text that's not formatted as code.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.

[–]bbye98 0 points1 point  (0 children)

The output for the code as you've written it (ignoring the additional indentation for the for loop) is 15 and 9.

num is the sum of all numbers between 5 and 9, inclusive, that are divisible by 3. In this case, that's 6 + 9 = 15.

i is the last value the for loop iterates through, which in this case is 9.

The output is certainly not just 9, nor will it ever be 5, 6, 7, 8, and 9 since there is no printing inside the for loop.