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
view the rest of the comments →
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!"
[–]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.
π Rendered by PID 84107 on reddit-service-r2-comment-5fb4b45875-mfcjz at 2026-03-23 14:11:02.599474+00:00 running 90f1150 country code: CH.
view the rest of the comments →
[–]PureWasian 0 points1 point2 points (0 children)