If you want 'while false' functionality, you need not. Try while not fn: instead.

Answer from Silas Ray on Stack Overflow
๐ŸŒ
TOOLSQA
toolsqa.com โ€บ python โ€บ python-while-loop
Python While Loop | While True and While Else in Python || ToolsQA
If there are any program requirements ... returns "False." After the "else" statement, the loop exits. ... i = 2 while(i <= 3): print("i is less than three") i = i + 1 else: print("i is now greater than three") The above code prints the following output onto the console: Alright! A good question to think here is, what happens when we have a break statement along with the else statement? Do else statements get skipped? Or does "else" get executed? If there is a while loop in python with a break ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how do while not loops work
r/learnpython on Reddit: How do while not loops work
September 11, 2024 -

I was reading through this code and I'm just not getting how while loops work when not operator is also used.

https://pastebin.com/5mfBhQSb

I thought the not operator just inversed whatever the value was but I just can't see this code working if that's the case.

For example , the while not sequenceCorrect should turn the original value of False to True, so the loop should run while it's True. But why not just state while True then? And why declare sequenceCorrect = True again? Doesn't it just means while True, make it True? And so on.

The only way it makes sense to me is of the while not loop always means False (just like the while always means as long as it's True) even if the value is supposed be False and should be inverted to True.

So, is that the case? Can anyone explain why it works like that?

Top answer
1 of 8
7
In general, recall the while loop syntax. while : Conceptually, coding aside, consider filling a glass of water. Is there space for more water in the glass? Okay, then pour some. Without going in detail of how this funky glass object is defined, hopefully the following makes sense as a python analog of that process. while glass.has_space(): #we're still filling the glass glass.add_water() #glass is now full of water Without writing out how this glass object works, let's say it has another method for returning a boolean (True/False) for whether or not it is currently full, instead of the previous example of whether or not it had space for more water. We can fill the glass in the same way, but we have to negate that statement using the not operator while not glass.is_full(): #we're still filling the glass glass.add_water() #glass is now full of water Going back to the initial syntax, which is still the same, the only thing we've done is changing the condition statement of the loop. Instead of checking that glass.has_space() evaluates to True, we are now checking that the expression not glass.is_full() evaluates to True. That is the same statement as evaluating that glass.is_full() evaluates to False, because the only thing the not operator does to a boolean is negating it, i.e. True becomes False and vice versa. Now looking at your code linked, the condition for looping is that not sequenceCorrect evaluates to True, which is equivalent to the statement that sequenceCorrect is False. I won't paste the code here, but we see on line 3 that sequenceCorrect starts its life being False, so upon entering the while loop we do indeed step into that block of code because at that time not sequenceCorrect is in fact True. Then the first thing we do on line 5 is reassign it to True. If the remaining lines don't change it back to False, this will stop the while loop from repeating, since in this current state not sequenceCorrect evaluates to False. So you can say that line 5 is defaulting the loop to not going to be repeated. The only way for the loop to be repeated is for lines 6-8 with the nested for loop and if statement to find some character in dna that is not in "actgn", upon which sequenceCorrect will be assigned to False and thus the statement in the while loop not sequenceCorrect would evaluate to True and thus the loop would repeat itself once more. Personally, I think this is just a pretty unclear way to achieve the goal of checking the validity of that input string. I would have defined it another way, but I can't see anything wrong with it really. If the not operation in the while loop statement is bothering you, you could equally have rewritten the code with a variable for instance named sequenceIncorrect and just flipped all assignments i.e. True's become False's and vice versa.
2 of 8
2
The difference here is that by using not sequenceCorrect you are avoiding the use of the break keyword. It's cleaner and makes the loop terminate itself, rather than relying on a specific keyword and force terminate it. You could use while True, but that way is not descriptive enough and can potentially create and endless loop if you don't take care of the edge cases. Think about it: while not sequenceCorrect is telling you, without a single comment line, that the loop should run while sequenceCorrect is False, vs while True which the only thing that's telling you is that this loop will run endlessly for unknown reasons.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_while_loops.asp
Python While Loops
With the break statement we can stop the loop even if the while condition is true: ... Note: The else block will NOT be executed if the loop is stopped by a break statement. ... If you want to use W3Schools services as an educational institution, ...
๐ŸŒ
Wikiversity
en.wikiversity.org โ€บ wiki โ€บ Python_Concepts โ€บ While_Statement
Python Concepts/While Statement - Wikiversity
Although the if statement from the previous lesson can serve many purposes, it isn't good at being recursive. This means that the said statement cannot loop over and over again. This is where the while statement comes into play. This statement will execute its code block over and over again ...
๐ŸŒ
Stanford CS
cs.stanford.edu โ€บ people โ€บ nick โ€บ py โ€บ python-while.html
While Loop
i = 99 while i < 10: print(i) i += 1 print('All done') # (zero iterations - no numbers print at all) All done ยท With a while-loop, it's possible to accidentally write a loop that never exits. In that case, the while just loops and loops but never makes the test False to exit.
๐ŸŒ
Real Python
realpython.com โ€บ python-while-loop
Python while Loops: Repeating Tasks Conditionally โ€“ Real Python
March 3, 2025 - The syntax is shown below: ... The code under the else clause will run only if the while loop terminates naturally without encountering a break statement. In other words, it executes when the loop condition becomes false, and only then.
Find elsewhere
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 64971 โ€บ what-is-use-of-while-false-whiletrue-and-how-to-use-it
What is use of while (false) while(true) and how to use it? | Sololearn: Learn to code for FREE!
while loops use only Boolean expression and when it is true. So when it gets true it'll execute until it gets false. while(false) means the condition is false which will end the loop.
๐ŸŒ
Python
wiki.python.org โ€บ moin โ€บ WhileLoop
While loops - Python Wiki
While loops, like the ForLoop, are used for repeating sections of code - but unlike a for loop, the while loop will not run n times, but until a defined condition is no longer met. If the condition is initially false, the loop body will not be executed at all.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ can someone explain what this `while true` function is actually checking?
r/learnpython on Reddit: Can someone explain what this `while True` function is actually checking?
December 13, 2023 -

https://pastebin.com/NWkKQc1P

It's from Automate the Boring Stuff. I tried running it through the python tutor and it didn't clarify. Is the basic point that there actually isn't anything it's checking to be True, so it's an infinite loop until you trigger the `break`?

PS I figure this is something simple from much earlier that I didn't internalize. I plan to go through all the basic curriculum again to make sure there aren't any gaps in super basic knowledge.

Top answer
1 of 8
23
A while loop runs while a condition is true. True is always true, so it will run until something causes it to break (e.g. a break statement, program crash, etc.) For comparison, the first loop in the code you posted could be rewritten without an infinite loop as: age = input("Enter your age: ") while not age.isdecimal(): age = input("Please enter a number for your age: ")
2 of 8
9
Correct; this is just a way to start an infinite loop that you intend to break with some condition/break statement later on. Mostly used for starting/running an application that you don't want to close down without user intent (clicking an exit/quit button in a UI or entering "quit", "exit", etc from the command line. It *isn't* a great construct to use when you want something that has solidly defined criteria to repeat until those criteria are met. As mopslik pointed out, you would be better served in those examples to actually use the defined criteria in those as your loop conditions since you're not at risk of just sticking the user in an infinite loop if you forget to write your break statement somewhere. You, also, don't have to write so many lines to accomplish the same goal. ``` password = "" while not password.isalnum(): password = input('Select a new password (alphanumeric only') print("Password is good") ``` You do not have to print a line and then call input(), either. Just put the message you want displayed into the parens for the function call: input("Enter your age: ")
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ while-loops-in-python-while-true-loop-statement-example
While Loops in Python โ€“ While True Loop Statement Example
July 19, 2022 - The general syntax for writing a while loop in Python looks like this: while condition: body of while loop containing code that does something ... You start the while loop by using the while keyword. Then, you add a condition which will be a Boolean expression. A Boolean expression is an expression that evaluates to either True or False...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ help understand while true/while false
r/learnpython on Reddit: Help understand while True/while False
November 27, 2013 -

for example, why does this code work:

user = input("Enter marital status: ")
user = user.upper()
valid = False
while not valid:
    if user == "M" or user == "S":
        valid = True
    else:
        print ("invalid input")
        user = input("Enter marital status: ")
        user = user.upper()

but this doesn't:

user = input("Enter marital status: ")
user = user.upper()
valid = True
while not valid:
    if user == "M" or user == "S":
        valid = False
    else:
        print ("invalid input")
        user = input("Enter marital status: ")
        user = user.upper()

isn't the boolean condition changing in both cases?

can someone just give me some guidelines for using while True/False,e.g. when is it best to use this type of a while loop? that I can follow on my test tomorrow (yes, tomorrow).

Thanks in advance!

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-do-while
Python Do While Loops - GeeksforGeeks
July 23, 2025 - In Python, we can simulate the ... condition is met. Do while loop is a type of control looping statement that can run any statement until the condition statement becomes false specified in the loop....
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ sorry to ask such a silly question but what does 'while true:' actually mean?
r/learnpython on Reddit: sorry to ask such a silly question but what does 'while True:' actually mean?
November 14, 2023 -

what does the True refer to?

eg if i write:

while True: 
    print("hi")

i know that "hi" will be repeated infinitely, but what is it thats True thats making "hi" get printed?

also, whatever it is thats True, im assuming its always true, so is that why if i typed 'while False', none of the code below would ever run?

sorry if this is a stupid question

edit: also, besides adding an if statement, is there anything else that would break this infinite while loop?

and how would i break the loop, lets say after "hi" has been printed 10 times, using an if statement or whatever else there is that can break it. thanks!

๐ŸŒ
Board Infinity
boardinfinity.com โ€บ blog โ€บ use-while-true-in-python
Use While True in Python | Board Infinity
August 13, 2025 - To use a "while True" loop in Python, you must first define a condition that will eventually evaluate to "False" for the loop to terminate. This is typically done using a Boolean variable initially set to "True," then modified within the loop ...