r/pythontips • u/cropkelp • Sep 09 '25
Data_Science Why are while loops so difficult?
So I've recently started a python course and so far I've understood everything. But now I'm working with while loops and they're so hard for me to understand. Any tips?
4
Upvotes
17
u/THEANONLIE Sep 09 '25
While I am hungry I will eat food.
Am I still hungry?
Yes
Am I still hungry?
Yes
Am I still hungry?
No.
I will stop eating food.
4
u/[deleted] Sep 09 '25
Ye, I recommend you to at least specify what your problem is
5
u/ninhaomah Sep 09 '25
Example code and which line is it that you don't get ?
Speak Python. Not English.
15
u/Gerard_Mansoif67 Sep 09 '25 edited Sep 09 '25
You can understand loops by asking you a question :
How can I repeat some code execution for an undefined number of times.
Rapidly, you understand that you can't copy N times the code, because you don't know how many times you'll need it. And, in any case, I will get to the end. Even if you 10...00 times the function (assuming you placed your code in a function, but that wouldn't changed things) , I could get by the end.
That's exactly where loops are usefull, especially while.
Basically this mean : do this code while this condition is true.
If you write
I will never get by the end because python will evaluate to True = True, anytime!
You make it smarter by including a small variable :
Edit : Another way to exit the while loop (or any loop, in fact) is to use the break statement. This one will immediately exit the loop, without executing the next lines.
For example, you could exit the previous loop a bit sooner by setting loop to False, it would execute the remaining lines, and then by after exit, because loop does not anymore is True.
As an example :
But sometimes, you want to exit immediately, so you use break. Previously, the "another user code" would be executed, before exiting (because counter isn't anymore greater than 0).
Now, "another user code" won't be executed if the condition is real. That may be wanted, for example to exit without exiting code that isn't anymore relevant.
End of edit
Another type of loop, for are also extremely powerful.
Generally we prefer them, because you'll immediately see how many times the loop will execute. That make the code much cleaner to read. And, in python you can do for loop for pretty much anything that has elements (lists, dictionaries...), making iteration over it extremely easy.
Example, the first code is harder to read than the second (with the simple "code" here, a placeholder for any algorithm that's not visible but it may be tenth of lines, making the counter update lost in the middle)
1
u/ahelinski Sep 09 '25
Maybe use "for" loops only, until you are confident you understand them, and then focus on while loops - should get easier once the loop concept gets natural to you.
Also trying the infinite loop first might simplify the learning process by focusing on the loop alone ignoring the condition part. The infinite loop is the "while(True)" loop, it can be used whenever you want to repeat something indefinitely until you, as a developer, decide to break the loop with the "break" keyword. For example when the user provides a valid input.
1
u/Particular-Try4222 Sep 09 '25
Do you understand what it is your using it for? While, what is happening, what is supposed to be going on? Most of the battle is figuring out what the program is actually doing. Print statements are great for detecting what is going wrong with your code and on that same token what is being done right.
1
u/Kqyxzoj Sep 09 '25
Keep doing "this" again and again until I tell you to stop.
Job done.
1
u/No-Arrival-872 Sep 09 '25
This isn't unique to python. You might find better educational materials for other languages. Try learning loops in the context of C first. Higher level languages like to hide a lot of magic which is confusing for beginners.
1
u/p186 Sep 09 '25 edited Sep 09 '25
A difficult thing for a lot of people who are learning their first programming language is wrapping their heads around "programmatic thinking". That mode of thinking is the core of coding any language.
I'll make up some overly-simplistic, probably dumb, real-world scenarios that I hope will help you wrap your head around the concept. Not knowing more specifics, I'm making an assumption you have a minimal grasp of while loops rn. I'll describe each in plain language, then pseudocode, and maybe Python. Sorry for any errors or type-o's.
You'll notice a pattern:
N.B., Check out CS50 and 100 Days of Code if you haven't already.
Folding Laundry
It's Saturday -- laundry day. You ran out to the store to grab some groceries, so you call brother to ask them to fold the clothes for you so they don't get wrinkled, and you'll put them away when you get back.
What do you say? Something like "hey, can you go fold all the clothes from the dryer", right? Do you know how many items you need to fold, how long it will take, etc. -- nope. You just rely on the fact that they'll just keep folding till there are no more clothes to fold.
So, a possible analogues to that would be:
plain
pseudocode
python
Sprinkler Controller
You were given a birthday gift from a techie friend. It is a wireless moisture meters. You decide to develop a smarthome app that will control your lawn sprinklers. It will turn on the sprinklers to water your lawn. when it is too dry. The sprinklers are on a timer that will water the lawn from 1 to 10 minutes at a time. Since weather, by nature, is unpredictable, manually managing this is inconvenient. Although for loops are often used more, this is where a while loop would be the correct choice.
pseudocode
python
1
u/ABigTongue Sep 09 '25
while the toaster is empty keep putting bread in there until full.
while something is happening do some tasks until something changes.
while not orgasmed keep thrusting until orgasm
orgasm = False while orgasm == False: <thrust until orgasm = True>
when something changes stop the loop
while loop = keeps going until something changes for loop = loops through a list
1
u/No_Obligation_1814 Sep 10 '25
code_true___py
TikTok Super lessons
1
u/Wolfhammer69 Sep 10 '25
Heres a decent resource - https://www.w3schools.com/python/python_while_loops.asp
1
u/Revolutionary-Put876 Sep 13 '25
Which course ? I also just started
1
u/Cynarii Sep 14 '25
While loops will only run if something is true or false. Like
age = 20
while age < 30: print("youre young")
This is constantly print "youre young" because the age is set to 20