If you want 'while false' functionality, you need not. Try while not fn: instead.
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.
Videos
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!
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.
Evaluate valid = True; not valid in your head.
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
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 :)
A while loop checks the condition (well, the expression) behind the while before each iteration and stops executing the loop body when the condition is False.
So while False means that the loop body will never execute. Everything inside the loop is "dead code". Python-3.x will go so far that it "optimizes" the while-loop away because of that:
def func():
i = 1
while False:
if i % 5 == 0:
break
i = i + 2
print(i)
import dis
dis.dis(func)
Gives the following:
Line Bytecode
2 0 LOAD_CONST 1 (1)
3 STORE_FAST 0 (i)
7 6 LOAD_GLOBAL 0 (print)
9 LOAD_FAST 0 (i)
12 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
15 POP_TOP
16 LOAD_CONST 0 (None)
19 RETURN_VALUE
That means the compiled function won't even know there has been a while loop (no instructions for line 3-6!), because there is no way that the while-loop could be executed.
while True:
means that it will loop forever.
while False:
means it won't execute.
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!