Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中停止While循环并退出脚本_Python_While Loop_Sikuli - Fatal编程技术网

在Python中停止While循环并退出脚本

在Python中停止While循环并退出脚本,python,while-loop,sikuli,Python,While Loop,Sikuli,我正在使用西库里OCR测试库。在我的Python脚本中,我正在寻找两个可能出现的图像之一。当其中一个出现时,if选择该对象 但是,我希望脚本结束。没有。我试过退出()和退出(),但都不行。在while循环停止并完成脚本之后,它工作正常 while True: if exists ("lose.png"): click ("lose.png") print ("***** YOU LOSE! *****") if exists ("win.png")

我正在使用西库里OCR测试库。在我的Python脚本中,我正在寻找两个可能出现的图像之一。当其中一个出现时,if选择该对象

但是,我希望脚本结束。没有。我试过退出()和退出(),但都不行。在while循环停止并完成脚本之后,它工作正常

while True:
    if exists ("lose.png"):
        click ("lose.png")
        print ("***** YOU LOSE! *****")
    if exists ("win.png"):
        click ("win.png")
        print ("***** YOU WIN! *****")
    StopIteration

quit()

您可以使用
中断退出任何循环

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
语句的计算结果都不是
True
,则循环将继续


StopIteration
是一个异常,通常由迭代器引发以表示它们已经完成。大多数使用它的Python代码只需要捕获该异常,但如果您想引发该异常,请使用
raisestopIteration()
语句。在这里这样做没有意义;您的脚本未作为迭代器运行,
StopIteration
异常将不会产生预期效果。

您可以使用
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
语句的计算结果都不是
True
,则循环将继续


StopIteration
是一个异常,通常由迭代器引发以表示它们已经完成。大多数使用它的Python代码只需要捕获该异常,但如果您想引发该异常,请使用
raisestopIteration()
语句。在这里这样做没有意义;您的脚本没有以迭代器的形式运行,
StopIteration
异常将不会产生预期效果。

您始终可以执行以下操作:

status = TRUE
while status:
        if exists ("lose.png"):
            click ("lose.png")
            print ("***** YOU LOSE! *****")
            status = FALSE
        if exists ("win.png"):
            click ("win.png")
            print ("***** YOU WIN! *****")
            status = FALSE
        StopIteration
    
    quit()

您始终可以这样做:

status = TRUE
while status:
        if exists ("lose.png"):
            click ("lose.png")
            print ("***** YOU LOSE! *****")
            status = FALSE
        if exists ("win.png"):
            click ("win.png")
            print ("***** YOU WIN! *****")
            status = FALSE
        StopIteration
    
    quit()

在Python中,
break
用于退出循环。要退出脚本,请使用
sys.exit()
。因此:

while True:
    if exists ("lose.png"):
        click ("lose.png")
        print ("***** YOU LOSE!*****")
    if exists ("win.png"):
        click ("win.png")
        print ("***** YOU WIN!*****")
    break // Exit the loop
import sys; sys.exit() // Close the program

在Python中,
break
用于退出循环。要退出脚本,请使用
sys.exit()
。因此:

while True:
    if exists ("lose.png"):
        click ("lose.png")
        print ("***** YOU LOSE!*****")
    if exists ("win.png"):
        click ("win.png")
        print ("***** YOU WIN!*****")
    break // Exit the loop
import sys; sys.exit() // Close the program

此外,在循环体中提升
StopIteration
,并不能真正实现您想要的功能;它必须在迭代器中引发。是。但这会停止对两个if语句的执行。在脚本找到两个图像中的一个之前,我不希望中断。使用break时,它使用-[info]退出代码停止脚本:0(我应该提到这一点),这就是我尝试使用StopIteration的原因。@Dave:我已经更新了答案,并将
break
语句移动到
if
语句中。只有当两个
if
语句之一为
True
时,
while
循环才会停止。文本直到I cmd+r才更新。谢谢你,玛蒂恩!这是if外部的中断造成的。此外,在循环体内部提升
StopIteration
,并不能真正实现您想要的功能;它必须在迭代器中引发。是。但这会停止对两个if语句的执行。在脚本找到两个图像中的一个之前,我不希望中断。使用break时,它使用-[info]退出代码停止脚本:0(我应该提到这一点),这就是我尝试使用StopIteration的原因。@Dave:我已经更新了答案,并将
break
语句移动到
if
语句中。只有当两个
if
语句之一为
True
时,
while
循环才会停止。文本直到I cmd+r才更新。谢谢你,玛蒂恩!如果这是由外部中断造成的。顺便说一句,您似乎没有使用OCR。这些是图像,而不是您试图检测的文本..顺便说一句,您似乎没有使用OCR。这些是图像,而不是您试图检测的文本。。