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.
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
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.
Python - While false loop - Stack Overflow
Python : While True or False - Stack Overflow
Help understand while True/while False
How do while not loops work
Videos
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.
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 :)
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!
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?
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.
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!