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.
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.
python - Using While True loops - Stack Overflow
python - Close a While True Loop from another While True - Stack Overflow
while True ?
What does "while True" mean?
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.
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"