You can exit any loop with break
:
while True:
if exists ("lose.png"):
click ("lose.png")
print ("***** YOU LOSE! *****")
break
if exists ("win.png"):
click ("win.png")
print ("***** YOU WIN! *****")
break
If neither of the if
statements evaluate to True
, the loop continues.
StopIteration
is an exception, usually raised by iterators to signal that they are done. Most Python code that uses it only needs to catch that exception, but if you wanted to raise it, use a raise StopIteration()
statement. There is no point in doing so here; your script is not being run as an iterator and the StopIteration
exception will not have the desired effect.