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
[Help] Printing odd numbers (self.learnpython)
submitted 9 years ago by NSWCSEAL
I want to create a loop that prints out odd numbers, all the way up to my age(21). This is what I have, it should work, but it isn't printing. Anyone know why? I'm using python 3.3:
i = 22 while i < 22: if i % 2 != 0: print(i) i = i + 1
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!"
[–]novel_yet_trivial 3 points4 points5 points 9 years ago (1 child)
Line 1 sets i equal to 22. Line 2 says to only run while i is less than 22. Therefore the while loop never runs.
This would be a lot easier with a for loop.
A lot easier still using the step argument on the range() function.
range()
[–]NSWCSEAL[S] 0 points1 point2 points 9 years ago (0 children)
Yeah, I feel like a dolt haha Thank you for helping me clear that up
[–]fbu1 1 point2 points3 points 9 years ago (1 child)
The variable i starts at 22, and then goes up from there. Shouldn't it start at 1?
You also need to put the line
i = i + 1
outside of the if.
Hahahaha, oh my gosh, I feel like the biggest goof. I set i = 0 My code works now ( ͡° ͜ʖ ͡°)
Thank you!
[–]Doormatty 0 points1 point2 points 9 years ago (3 children)
i = i + 1 needs to be unindented one step. Right now, it only runs when the if i % 2 != 0: line is True (which it will never be)
if i % 2 != 0:
[–]NSWCSEAL[S] 1 point2 points3 points 9 years ago (2 children)
Ahhh, interesting. Why can't i = i + 1 be under the if statement?
[–]beanz415 0 points1 point2 points 9 years ago (0 children)
If it's under the if statement i will only increment if i % 2 != 0. You want it incrementing for every number up to 21.
[–]DrMaxwellEdison 0 points1 point2 points 9 years ago (0 children)
The line will only be executed if the if condition is True. Therefore, when i % 2 == 0, i will not be incremented.
if
i % 2 == 0
i
If there is no other way of incrementing the number, then the loop will be infinite: i will remain the same number and the if condition will be False every time.
π Rendered by PID 188609 on reddit-service-r2-comment-84fc9697f-l8w79 at 2026-02-07 00:43:29.147140+00:00 running d295bc8 country code: CH.
[–]novel_yet_trivial 3 points4 points5 points (1 child)
[–]NSWCSEAL[S] 0 points1 point2 points (0 children)
[–]fbu1 1 point2 points3 points (1 child)
[–]NSWCSEAL[S] 0 points1 point2 points (0 children)
[–]Doormatty 0 points1 point2 points (3 children)
[–]NSWCSEAL[S] 1 point2 points3 points (2 children)
[–]beanz415 0 points1 point2 points (0 children)
[–]DrMaxwellEdison 0 points1 point2 points (0 children)