[Tutor] Unable to get out of loop

Steven D'Aprano steve at pearwood.info
Fri Feb 26 21:26:07 EST 2016


On Fri, Feb 26, 2016 at 08:30:19PM -0500, Ken G. wrote:

> side = ""
> while side != "0" or side != "1" or side != "2":

That will only exit if side == "0", "1" and "2" **at the same time**. 
Clearly that is impossible, so the condition is always true, and the 
while loop will never end.

Trying to reason about "reversed conditions" like that is hard, much 
much harder than reasoning about ordinary conditions. You should 
re-write it in terms of equality, then reverse the condition once:

while side not in ("0", "1", "2):
    ...


-- 
Steve


More information about the Tutor mailing list