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?
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.
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.
There is no do-while loop in Python.
This is a similar construct, taken from the link above.
while True:
do_something()
if condition():
break
I prefer to use a looping variable, as it tends to read a bit nicer than just "while 1:", and no ugly-looking break statement:
finished = False
while not finished:
... do something...
finished = evaluate_end_condition()
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.
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 :)
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!