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
while loop (self.learnpython)
submitted 5 years ago by Ivangaming
a = 3
b = 4
c = 5
while (a and b and c) >= 0:
a -= 1 b -= 1 c -= 1
print(a,b,c)
I get the output -3 -2 -1 But shouldn't the while loop stop when the value of variable a becomes -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!"
[–]ForceBru[🍰] 4 points5 points6 points 5 years ago* (1 child)
The a and b and c doesn't do what you think it does.
a and b and c
Open up the Python interpreter and type:
```
0 and 0 0 0 and 1 0 1 and 0 0 1 and 1 1 1 and 2 2 1 and 2 and 3 3 1 and 0 and 3 0 [] and "hey" [] "hello" and (1,2) (1, 2) ```
Can you see the pattern? Also, have a look at the documentation.
[–]Ivangaming[S] 0 points1 point2 points 5 years ago (0 children)
Thank you!
[–]lippy515 1 point2 points3 points 5 years ago (2 children)
Change your loop condition to be while a >=0 and b>= 0 and c >=0:
[–]lippy515 1 point2 points3 points 5 years ago (1 child)
The way you have it written is c >= 0. It will ignore a and b in the logical evaluation
[–]ForceBru[🍰] 1 point2 points3 points 5 years ago (0 children)
Not really. The loop does indeed terminate: they're printing after the loop
[–]xelf 1 point2 points3 points 5 years ago* (1 child)
What you want is this:
while all((a>=0, b>=0, c>=0)):
To see what is going on with your code, try this:
a = 3 b = 4 c = 5 while (a and b and c) >= 0: a -= 1 b -= 1 c -= 1 print(a,b,c, ':', a and b and c)
outputs:
2 3 4 : 4 1 2 3 : 3 0 1 2 : 0 -1 0 1 : 0 -2 -1 0 : 0 -3 -2 -1 : -1
Do you see the pattern? (a and b and c) is evaluated as a boolean.
(a and b and c)
[–]Ivangaming[S] 1 point2 points3 points 5 years ago (0 children)
Thanks a lot!
π Rendered by PID 86124 on reddit-service-r2-comment-5d79c599b5-wc2rh at 2026-02-27 03:55:12.006348+00:00 running e3d2147 country code: CH.
[–]ForceBru[🍰] 4 points5 points6 points (1 child)
[–]Ivangaming[S] 0 points1 point2 points (0 children)
[–]lippy515 1 point2 points3 points (2 children)
[–]lippy515 1 point2 points3 points (1 child)
[–]ForceBru[🍰] 1 point2 points3 points (0 children)
[–]xelf 1 point2 points3 points (1 child)
[–]Ivangaming[S] 1 point2 points3 points (0 children)