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
Find elsewhere
๐ŸŒ
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.
Top answer
1 of 2
5

Since your while not pair: loop did not work, you have found an important difference: pair == False only tests true if pair is set to 0 or False (the only two values that test as equal to False), while while not pair tests for any truth value (inverting that value).

You appear to have assigned some other value to pair that is neither of those two values causing the behaviour to change (a truthy value to break out early, or a falsey value to keep the loop going longer than expected).

It is that difference that is one of the reasons why the Python style guide recommends you always use if true_expression or if not false_expression rather than use == True or == False:

  • Don't compare boolean values to True or False using ==.

    Yes: if greeting:
    No: if greeting == True:
    Worse: if greeting is True:

Last but not least, for a while ...: loop that simply tests against a single boolean flag (while flag: or while not pair:), consider using while True: and break instead. So rather than do:

flag = True
while flag:
    # ...
    if condition:
        flag = False

do this instead:

while True:
    # ...
    if condition:
        break
2 of 2
0

Aside from actually having little or no difference at all,

Using False in a == comparison allows usage of 0 and 1.

0 == False
1 == True

Using not is inversion of current value.

not 0 == True
not 1 == False
not False == True

You can use in your program assuming that pair can only contain boolean values:

while not pair:

If you however still want to use a variable that can contain both boolean and number, you can use:

while pair is False:
Top answer
1 of 4
1

To expand on what others have already stated, run on these lines

if loop == "y" :
    run = True
elif loop == "n" :
    run = False

are not referring to the same run defined by

#Can be set to True if while (run != True) is set to while (run == True).
run = False

run in the cont function is a local variable to your function, not the globaly defined run.

There are a couple (at least) ways to fix this. The preferred (imo) way to do it is have cont return a new value to be assigned to run. That would look like

#Defining cont(). Ask for imput and error handling.
def cont(_run):
    loop = input("Would you like to convert another word? (y/n): ").lower()
    if loop == "y" :
        return _run
    elif loop == "n" :
        return not _run
    else :
        print("You did not enter a valid response, please try again.")
        return cont(_run)

...

#Infinite loop as long as run is not equal to True.
while (run != True) :
    translate()
    run = cont(run)

The other (less preferred) way would be to use the global run variable inside of your cont function. This is achieved using the global keyword.

That would look like this:

#Defining cont(). Ask for imput and error handling.
def cont():
    global run
    loop = input("Would you like to convert another word? (y/n): ").lower()
    if loop == "y" :
        run = True
    elif loop == "n" :
        run = False
        print("Thank you for using this program, have a nice day!")
        exit()
    else :
        print("You did not enter a valid response, please try again.")
        cont()

** Couple side notes
In my first example I return _run when the value is y and not _run when the value is n. This allows you to change you initial run value to be True, and change the while condition without having to change the cont function itself.

You don't need to actually change the run value at all if you use the global and the user enters n since you exit before the function returns.

You might be better off changing your if conditional checks to

if loop in ("yes", "y"):
if loop in ("no", "n"):

since lots of people don't read full instructions :)

2 of 4
1

The run inside the cont function is a local variable. Changing its value has no effect on the global variable that the while loop refers to.

๐ŸŒ
Python
wiki.python.org โ€บ moin โ€บ WhileLoop
While loops - Python Wiki
April 10, 2017 - Starting in Python 3, True, False, and None are keywords, so using while 1 no longer provides the tiny performance benefit used to justify it in earlier versions. See also: http://stackoverflow.com/questions/3815359/while-1-vs-for-whiletrue-why-is-there-a-difference
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 74106173 โ€บ how-do-i-rewrite-this-while-loop-without-it-being-a-while-true-loop
python - How do I rewrite this while loop without it being a while True loop? - Stack Overflow
Explore Stack Internal ... For a class assignment we are supposed to write a function that prompts the user for input, checks that the user inputed a valid integer between the range of lo and hi and returns the result. We have to print the correct prompts in failure cases and use the provided is_int(str) helper function provided. I've written a version that I feel is fairly concise but the professor has said to avoid while True loops.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 54226698 โ€บ while-loops-in-python-3-and-true-false-conditions
While loops in Python 3 and True/False conditions - Stack Overflow
True and False are getting evaluated as themselves. while False: never enters the loop... while True never terminates unless you break or return from inside the loop (return only in a function, fo course) ...