Yes!

Once the condition has been met, use break:

while True:
    someVar = str.lower(input())

    if someVar == 'yes':
        someVar = True
        break
    if someVar == 'no':
        someVar = False
        break

You can also use while False with:

met = True

while met:

   someVar = str.lower(input())

   if someVar == 'yes':
        someVar = True
        break
    if someVar == 'no':
        someVar = False
        break

Since all strings are True, use another variable to store either True or False. Once met is false, then the loop will break.

Answer from Anthony Pham on Stack Overflow
Discussions

Python - While false loop - Stack Overflow
fn='a' x=1 while fn: print(x) x+=1 if x==100: fn='' Output: 1 ... 99 fn='' x=1 while fn: print(x) x+=1 if x==100: fn='a' Output: while loop does not run. ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Python : While True or False - Stack Overflow
@MartijnPieters You might be right ... only True False conditions his answer would be better for them. 2014-08-25T11:14:56.26Z+00:00 ... @TolgaVarol: I disagree with that; the correct way to solve the problem you are trying to solve is to use break. I tried to both answer why your code didn't work and how to do it correctly; both are important. 2014-08-25T11:20:54.783Z+00:00 ... This answer worked for me because I have an inner for loop, so assigning a variable to the outer while statement ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Help understand while True/while False
When you say the code doesn't work, I assume you mean the code inside the while loop doesn't execute in the 2nd example. While loops in python have format while BOOLEAN: CODE where BOOLEAN is an expression which evaluates to either True or False. If the BOOLEAN evaluates to True, then CODE is executed. If not, CODE is skipped over. In your second example, BOOLEAN is the expression not valid valid has value True so, not valid is not True which evalutes to False. Therefore the CODE doesn't execute. More on reddit.com
๐ŸŒ r/learnpython
7
3
December 19, 2015
How do while not loops work
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. More on reddit.com
๐ŸŒ r/learnpython
16
5
September 11, 2024
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ trouble understanding while not
r/learnpython on Reddit: Trouble understanding While Not
February 16, 2017 -

I'm doing "automate the boring stuff" and was on an early lesson about while loops. I can't wrap my head around "while not." The code is below

name = ''
while not name:
     print('Enter your name:')
     name = input()
print('How many guests will you have?')

While my limited knowledge, I would write it instead as

name = ''
while name == '':
    print('Enter your name:')
    name = input()
print('How many guests will you have?')

My question is how can they mean the same thing? Both start with name = False/empty. The way I interpret the top code is, while name is not False/empty. The way I interpret the bottom is, while name is equal to False/empty. In my mind, the top would keep looping with any True value. Thanks for your help!

EDIT: formatting

EDIT 2: Finally figured it out. I'm posting this edit for prosperity sake, for any other programming ogres like me. As long as the condition line is True, the loop will run. If the condition is False, the loop will not run. Even though name is False, the condition line is True and the loop will go until something is inputted. Once something is typed in, name becomes True, which would make the condition not name False, ending the loop.

Thanks everyone for helping me understand that! It's so simple now that I see it. I'm super new to Python and made a rookie mistake.

๐ŸŒ
Stanford CS
cs.stanford.edu โ€บ people โ€บ nick โ€บ py โ€บ python-while.html
While Loop
It's better style to write it the following way, letting the while itself evaluate the True/False of the test: ... The break and continue directives work the same as in the for-loop (See also for loop). The break provides an extra way to exit the loop. Typically the break is nested inside an if. The if/break can be positioned anywhere in the loop vs. the while/test can only be at the top of the loop. Here is the previous example with a break added to leave the loop if the number is 6: i = 0 while i < 10: print(i) if i == 6: break i = i + 1 print('All done') 0 1 2 3 4 5 6 All done
๐ŸŒ
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, ...
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ what is โ€œnot trueโ€ in python?
What is "not True" in Python? - AskPython
February 27, 2022 - This example is alone sufficient to prove how โ€œnotโ€ is useful: ... The code will not run. The while loop iterates the code if and only if the condition inside its parenthesis is True...
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ help understand while true/while false
r/learnpython on Reddit: Help understand while True/while False
December 19, 2015 -

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!

๐ŸŒ
Wikiversity
en.wikiversity.org โ€บ wiki โ€บ Python_Concepts โ€บ While_Statement
Python Concepts/While Statement - Wikiversity
April 28, 2024 - 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 ...
๐ŸŒ
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.
๐ŸŒ
Python
wiki.python.org โ€บ moin โ€บ WhileLoop
While loops - Python Wiki
April 10, 2017 - If the condition is initially false, the loop body will not be executed at all. As the for loop in Python is so powerful, while is rarely used, except in cases where a user's input is required*, for example: n = raw_input("Please enter 'hello':") while n.strip() != 'hello': n = raw_input("Please enter 'hello':") However, the problem with the above code is that it's wasteful. In fact, what you will see a lot of in Python is the following: while True: n = raw_input("Please enter 'hello':") if n.strip() == 'hello': break
๐ŸŒ
Real Python
realpython.com โ€บ lessons โ€บ not-in-while-loops
Using not in Boolean while Loops (Video) โ€“ Real Python
00:32 So you can use a while loop using a not condition to ensure that this happens. Hereโ€™s a simple example. The player is supposed to enter a name at the input() statement. 00:44 This action will repeat until the player types something at the prompt. Remember the empty string ("") has a truth value of False, so not name will return True if name is still empty.
Published ย  July 19, 2022
๐ŸŒ
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.
๐ŸŒ
TOOLSQA
toolsqa.com โ€บ python โ€บ python-while-loop
Python While Loop | While True and While Else in Python || ToolsQA
August 6, 2021 - Your loop will continuously eat the resources and waste time. Typically, while true in python is used with a nested if-else block, but this is not standard, and there is no such rule. Every program has its demands, and as you work your way forward, you will be able to implement this with variations.
๐ŸŒ
Coursera
coursera.org โ€บ tutorials โ€บ python-while-loop
How to Write and Use Python While Loops | Coursera
Python will interpret all non-zero values as true while none and 0 are false. ... In this example, the loop will print out the even numbers from 0 to 8, because we start at 0 and add 2 to the number for each iteration. Once num reaches 10, the condition num < 10 is no longer true, and the loop terminates. ... Be careful not ...
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 65293083 โ€บ how-to-properly-use-while-not-loops-in-python
How to properly use "while not" loops in python? - Stack Overflow
The best explanation for while not guessed is that it is essentially saying "while guessed is not equal to true". This is to say it is basically "while guessed is false." So the while loop will run for as long as guessed is false and tries > 0.
๐ŸŒ
Real Python
realpython.com โ€บ python-while-loop
Python while Loops: Repeating Tasks Conditionally โ€“ Real Python
March 3, 2025 - The bad news is that Python doesnโ€™t have one. The good news is that you can emulate it using a while loop with a break statement. Consider the following example, which takes user input in a loop: ... >>> while True: ... number = int(input("Enter a positive number: ")) ... print(number) ... if not number > 0: ...
๐ŸŒ
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!