如何跳出 python 中的嵌套循环?

How do I break out of a nested loop in python?

如果在屏幕上找到图像 a.png,我将触发循环。

这是有效的,它循环直到找到 a.png,然后 主查找循环 开始。

一旦 Main Find Loop else 触发,我想跳转到上一个循环并重新开始。

我试过将 break 放在我认为应该停止并返回第一个循环的地方,但它没有中断; 主查找循环继续。

我认为在我的 else 之后添加 break 会在执行 else 时停止该循环。

我只是将计数用于测试目的,目的是让这些成为无限循环。

while CountA <20:

        if pyautogui.locateOnScreen('a.png') != None:

        image_list = []
        # Get list of all files in current directory
        directory = os.listdir()

        # Find files that end with .png or .jpg and add to image_list
        for file in directory:
            if file.endswith('.png') or file.endswith('.jpg'):
                image_list.append(file)

        #Loop through the list to find all the images
            count = 0
            while count <20:   

                #Main Find Loop
                for image in image_list:
                    name = image
                    Found = pyautogui.locateCenterOnScreen(image)
                    if Found != None:
                        pyautogui.moveTo(1700,1000,0.1)
                        pyautogui.click()
                    else:
                        pyautogui.moveTo(1415,1000,0.1)
                        pyautogui.click()
                        break

            count = count + 1

CountA = countA + 1

根据您在主 post 下的回复,此代码适用于您的用例。

while CountA <20:

        if pyautogui.locateOnScreen('a.png') != None:

        image_list = []
        # Get list of all files in current directory
        directory = os.listdir()

        breaking_bool = False
        # Find files that end with .png or .jpg and add to image_list
        for file in directory:
            if breaking_bool is True:
                break
            if file.endswith('.png') or file.endswith('.jpg'):
                image_list.append(file)

        #Loop through the list to find all the images
            count = 0
            while count <20 and breaking_bool is False:   

                #Main Find Loop
                for image in image_list:
                    name = image
                    Found = pyautogui.locateCenterOnScreen(image)
                    if Found != None:
                        pyautogui.moveTo(1700,1000,0.1)
                        pyautogui.click()
                    else:
                        pyautogui.moveTo(1415,1000,0.1)
                        pyautogui.click()
                        breaking_bool = True
                        break

            count = count + 1

CountA = countA + 1

这个想法是引入一个布尔变量(我称它为 breaking_bool),它会告诉你调用的“先前的外循环”在什么时候停止这是真的。

从内循环中脱离外循环的最简单方法就是不要外循环。

因为在你的外循环中什么都没有发生,你可以用 itertools.cycle 替换它(对于 while True,因为你说计数器只是为了调试),或者一个有限重复的生成器表达式(或 itertools.repeatchain.from_iterable 的组合,但我认为生成器表达式更容易)。

示例:无限循环 image_list 直到条件为真:

for image in itertools.cycle(image_list):
    Found = pyautogui.locateCenterOnScreen(image)
    if Found != None:
        pyautogui.moveTo(1700,1000,0.1)
        pyautogui.click()
    else:
        pyautogui.moveTo(1415,1000,0.1)
        pyautogui.click()
        break

示例:循环 image_list 最多 20 次或直到条件为真:

for image in (img for _ in range(20) for img in image_list):
    Found = pyautogui.locateCenterOnScreen(image)
    if Found != None:
        pyautogui.moveTo(1700,1000,0.1)
        pyautogui.click()
    else:
        pyautogui.moveTo(1415,1000,0.1)
        pyautogui.click()
        break