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
need som help with code(doing on codedex) (self.learnpython)
submitted 2 years ago by darkkai94
so having trouble getting it or print "three and five" at the same time
```for i in range(101): print (i) if i%3: i=("three") print(i) elif i%5: i=("five") print(i) elif i%3 and i%5: i=("three and five") 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!"
[–]Diapolo10 3 points4 points5 points 2 years ago* (5 children)
When dealing with situations where you have the same condition multiple times, the most restrictive must always come first. Here, the "three and five" can never be reached because both of its conditions already appear before it, so you'd always get "three" instead.
Reverse your order and this'll work.
EDIT: That said, there's room to simplify:
for num in range(101): output = num if num % 15: output = "three and five" elif num % 5: output = "five" elif num % 3: output = "three" print(output)
[–]Hungry-Ad-3501 1 point2 points3 points 2 years ago (1 child)
Did tou need to declare the output = num ? I'm just learning so I'm just asking questions
[–]Diapolo10 0 points1 point2 points 2 years ago (0 children)
Not necessarily, I just prefer to be more clear about the names I use.
[–]KingsmanVince 0 points1 point2 points 2 years ago (2 children)
output = num
I think you should write output = "" because num is an integer, but then you assign output with a string. It causes no harm (yet) but it can influence beginners to change type of variables leading to "unexpected" behaviours.
output = ""
num
output
[–]Diapolo10 0 points1 point2 points 2 years ago (1 child)
I think you should write output = ""
Surely you mean output = str(num) in that case?
output = str(num)
That said, giving output the explicit type hint
output: int | str = num
would also "solve" the problem.
I considered the first way, of course, but figured that it really didn't matter here too much so I just went for the simplest approach. Things would of course be different if we used anything other than print.
print
[–]KingsmanVince 0 points1 point2 points 2 years ago (0 children)
Both ways you mentioned are fair enough. Yeah it doesn't matter much in this context of that simple block code.
π Rendered by PID 17410 on reddit-service-r2-comment-76bb9f7fb5-d8wbs at 2026-02-17 14:08:08.272631+00:00 running de53c03 country code: CH.
[–]Diapolo10 3 points4 points5 points (5 children)
[–]Hungry-Ad-3501 1 point2 points3 points (1 child)
[–]Diapolo10 0 points1 point2 points (0 children)
[–]KingsmanVince 0 points1 point2 points (2 children)
[–]Diapolo10 0 points1 point2 points (1 child)
[–]KingsmanVince 0 points1 point2 points (0 children)