Use break to exit a loop:
while True:
ser = serial.Serial("/dev/ttyUSB0", 4800, timeout =1)
checking = ser.readline();
if checking.find(",,,,"):
print "not locked yet"
else:
print "locked and loaded"
break
The True and False line didn't do anything in your code; they are just referencing the built-in boolean values without assigning them anywhere.
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.
Use break to exit a loop:
while True:
ser = serial.Serial("/dev/ttyUSB0", 4800, timeout =1)
checking = ser.readline();
if checking.find(",,,,"):
print "not locked yet"
else:
print "locked and loaded"
break
The True and False line didn't do anything in your code; they are just referencing the built-in boolean values without assigning them anywhere.
You can use a variable as condition for your while loop instead of just while True. That way you can change the condition.
So instead of having this code:
while True:
...
if ...:
True
else:
False
... try this:
keepGoing = True
while keepGoing:
ser = serial.Serial("/dev/ttyUSB0", 4800, timeout =1)
checking = ser.readline();
if checking.find(",,,,"):
print "not locked yet"
keepGoing = True
else:
keepGoing = False
print "locked and loaded"
EDIT:
Or as another answerer suggests, you can just break out of the loop :)
Videos
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.
This should work
someVar = None
while (True):
someVar = str.lower(input())
if someVar == 'yes':
someVar = True
break
elif someVar == 'no':
someVar = False
break
Have a look on a code below:
def repeat():
correct_value = False
# while not False so the correct_value = True
# so it is an equivalent of -> while True:
# while True will always run
while not correct_value:
try:
guess = int(input())
except ValueError:
print("Please Enter a Number")
else:
correct_value = True
return guess
Why does "while not false" even loops in the first place?
It loops because of the double negation which is always True.
And what's the actual really keeps the loop running?
Exactly the same while not False, which results in while True
not False means True, that's why it works in the first place. Exceptional handling is used for purposes like this. It tries to take an integer input, if the user does not enter an integer, it throws an error which is handled by the except block.
if the user does give an integer, correct_value becomes true, so not True means false so the loop terminates and the function returns the input.
If you want 'while false' functionality, you need not. Try while not fn: instead.
The condition is the loop is actually a "pre-" condition (as opposed to post-condition "do-while" loop in, say, C). It tests the condition for each iteration including the first one.
On first iteration the condition is false, thus the loop is ended immediately.
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?
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!
while True means loop forever. The while statement takes an expression and executes the loop body while the expression evaluates to (boolean) "true". True always evaluates to boolean "true" and thus executes the loop body indefinitely. It's an idiom that you'll just get used to eventually! Most languages you're likely to encounter have equivalent idioms.
Note that most languages usually have some mechanism for breaking out of the loop early. In the case of Python it's the break statement in the cmd == 'e' case of the sample in your question.
my question: while WHAT is True?
While True is True.
The while loop will run as long as the conditional expression evaluates to True.
Since True always evaluates to True, the loop will run indefinitely, until something within the loop returns or breaks.
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
TrueorFalseusing==.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
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: