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.
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.
Can someone explain what this `while True` function is actually checking?
sorry to ask such a silly question but what does 'while True:' actually mean?
What actually "while true" statement mean can someone pull me out this?
python - Using While True loops - Stack Overflow
Videos
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.
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!
If you're determined to use a while loop for some reason, it'd be something like this:
i = 0
while True:
i += 1
word = words[i]
if (syllables in word):
print('Syllables are in word')
else:
print('Syllables not in word')
if i > len(words):
break
However, this is probably a much worse way to do solve the issue. When iterating through a set it's often more efficient to use a for loop to prevent needless checking of whether the entire set has been checked. A for loop implementation might look like this:
for s in syllable_set_1:
if s in word:
print('Syllable ' + s + ' is in word')
else:
print('Syllable ' + s + ' is not in word')
For overlapping syllables you might actually require some kind of backtracking. A simple solution could use while ... else, like this:
syllables = ["in", "ex"]
word = "exinex"
while word:
matches = [s for s in syllables if word.startswith(s)]
if not matches:
print "unable to find syllable for " + word
break
word = word.replace(matches[0], "", 1)
else:
print "only expected syllables found"
I remember seeing a video where the programmer said that using while True loops aren't good.
Can someone give an example of where this might be the case?
What would be better to use? especially, if I am trying to check for user input.
E.g. They have entered a username that is not in the list of usernames.