I am using Sikuli the OCR testing library. In my Python script I am looking for one of two possible images to appear. When one of them does appear, if chooses that object.
However, I would like for the script to end. It doesn't. I've tried quit()
and exit()
but that isn't doing it. It is working fine, beside the stop of the while loop and completing the script.
while True:
if exists ("lose.png"):
click ("lose.png")
print ("***** YOU LOSE! *****")
if exists ("win.png"):
click ("win.png")
print ("***** YOU WIN! *****")
StopIteration
quit()
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.