Using not in Non-Boolean Contexts
A non-Boolean context is somewhere other than the condition of an
if statement or
while loop, usually when you want to change the value of a Boolean variable, often to indicate that the program has changed state in some way, and that Boolean variable will be tested in some upcoming
while statement to determine that.
So an initial attempt might look to something like this. The
toggle variable is supposed to alternate between
False. If it’s
True, you do one set of instructions. If it’s
False, you do something else.
At the end of when it’s
True, it gets set to
False. And at the end of when it’s
False, it gets set to
True. Remember, the test in a
while loop is only checked at the beginning of each iteration.
It still does the
# Do something... part if
True and the
# Do something else... part if
False, but now there is just a single assignment statement setting
toggle to the
not of what it currently is.
Become a Member to join the conversation.