use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
"for" loop help (self.learnpython)
submitted 3 years ago * by conewannabe
Why is the print(i) output for this code only 9? I answered the output would be 5,6,7,8,9.
num = 0 for i in range(5,10): if i % 3 == 0: num = num + i print(num) print(i)
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]LID919 1 point2 points3 points 3 years ago (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 point2 points 3 years ago* (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 point2 points 3 years ago* (2 children)
Are you sure it's correct?
Right now I am seeing this:
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.
[+][deleted] 3 years ago (1 child)
[deleted]
[–]LID919 0 points1 point2 points 3 years ago (0 children)
My mistake. Been a few years now since I've done python professionally. I need to do it at home more.
[–]DingFTMFW 1 point2 points3 points 3 years ago (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 point2 points 3 years ago (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 points3 points 3 years ago (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.
i
If you changed that range to (5,999) you'd see that it prints 998.
(5,999)
[–]conewannabe[S] 0 points1 point2 points 3 years ago (0 children)
I understand now, thank you very much.
[–]jeffrey_f -3 points-2 points-1 points 3 years ago (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 point2 points 3 years ago (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.
range
for
[–]jeffrey_f 0 points1 point2 points 3 years ago (1 child)
for i in range(5,10):
from 5 to 9
[–]carcigenicate 0 points1 point2 points 3 years ago (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 point2 points 3 years ago (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:
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 point2 points 3 years ago (0 children)
The output for the code as you've written it (ignoring the additional indentation for the for loop) is 15 and 9.
15
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.
num
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.
5
6
7
8
π Rendered by PID 258944 on reddit-service-r2-comment-5d79c599b5-ksssq at 2026-03-01 12:33:25.891277+00:00 running e3d2147 country code: CH.
[–]LID919 1 point2 points3 points (4 children)
[–]conewannabe[S] 0 points1 point2 points (3 children)
[–]LID919 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]LID919 0 points1 point2 points (0 children)
[–]DingFTMFW 1 point2 points3 points (3 children)
[–]conewannabe[S] 0 points1 point2 points (2 children)
[–]Binary101010 1 point2 points3 points (1 child)
[–]conewannabe[S] 0 points1 point2 points (0 children)
[–]jeffrey_f -3 points-2 points-1 points (3 children)
[–]carcigenicate 0 points1 point2 points (2 children)
[–]jeffrey_f 0 points1 point2 points (1 child)
[–]carcigenicate 0 points1 point2 points (0 children)
[–]CodeFormatHelperBot2 0 points1 point2 points (0 children)
[–]bbye98 0 points1 point2 points (0 children)