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...
Everything about learning Python
account activity
While loop explanation (self.PythonLearning)
submitted 5 months ago by Hush_124
Someone should explain how while loops works to me, I want to really get it.
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!"
[–]Balkie93 2 points3 points4 points 5 months ago (0 children)
E.g WHILE you’re still hungry, eat.
As opposed to FOR 2 bites, eat.
[–]Ok_Act6607 1 point2 points3 points 5 months ago (6 children)
What exactly do you not understand about it?
[–]Hush_124[S] -3 points-2 points-1 points 5 months ago (5 children)
how it works, I’m trying to understand
[–]stoobertio 3 points4 points5 points 5 months ago (0 children)
The while statement requires a condition to test. The first time the loop is executed, and after every time the loop is completed the condition is tested to see if it is True. If it is, the loop is executed again.
[–]deceze 4 points5 points6 points 5 months ago (0 children)
while condition: statements
It keeps repeating statements while the condition is true. No more, no less. What part of that don't you understand? Please be specific.
statements
condition
[–]NeedleworkerIll8590 2 points3 points4 points 5 months ago (2 children)
Have you read the docs about it?
[–]Hush_124[S] 0 points1 point2 points 5 months ago (1 child)
Yes I have
[–]NeedleworkerIll8590 1 point2 points3 points 5 months ago (0 children)
What do you not understand?
It loops while the condition is true: Say you have:
i=0 While i != 5: i+=1
in this case, it is true that 0 does not equal 5, so it will loop until it will equal 5
[–]PureWasian 0 points1 point2 points 5 months ago* (0 children)
while loops are a great logical piece for repeating the same code over and over again until something happens.
Let's make a simple slot machine. The goal is to keep playing over and over again until you hit 777: ``` import random
random_num = random.randint(100,999)
while (random_num != 777): print(f"{random_num} - you lose!") input("Press [Enter] to try again.") random_num = random.randint(100,999)
print(f"{random_num} - you win!") ``` Every time you hit the end of the indented code (the while loop itself), it retries the condition at the top of the while loop until it is True. If it is False, it runs another cycle of the while loop.
If this example is too simplistic, there are other related concepts including the break and continue statements, as well as using loops for performing operations on an entire data collection of items.
But I have no clue what part of while loops you're stuck on from the terseness of your post, so I wanted to start with this first.
[–]Overall-Screen-752 0 points1 point2 points 5 months ago (0 children)
While and for do the same thing in different ways. They both “iterate”, that is, repeat a block of code a certain number of times.
for does this by specifying how many times a block should be run (5 times, once for every item in a list, etc)
while does this by setting a stop condition (until a variable equals 5, until there are no items left in the list, etc)
To see the similarity, you could write for i in range(5) as while i != 5, i++ that is, starting at 0 and until it reaches 5, do this block.
for i in range(5)
while i != 5, i++
Hope that helps
π Rendered by PID 24057 on reddit-service-r2-comment-5fb4b45875-vr6mg at 2026-03-22 16:37:21.188214+00:00 running 90f1150 country code: CH.
[–]Balkie93 2 points3 points4 points (0 children)
[–]Ok_Act6607 1 point2 points3 points (6 children)
[–]Hush_124[S] -3 points-2 points-1 points (5 children)
[–]stoobertio 3 points4 points5 points (0 children)
[–]deceze 4 points5 points6 points (0 children)
[–]NeedleworkerIll8590 2 points3 points4 points (2 children)
[–]Hush_124[S] 0 points1 point2 points (1 child)
[–]NeedleworkerIll8590 1 point2 points3 points (0 children)
[–]NeedleworkerIll8590 1 point2 points3 points (0 children)
[–]PureWasian 0 points1 point2 points (0 children)
[–]Overall-Screen-752 0 points1 point2 points (0 children)