I want to iterate over an entire array of images and if any of them are found, I want to click x
If I get to the end of the array and none of them is found, I want to click y
and then break out of the loop.
I can't figure out how to iterate over the entire array; this works but it iterates over the images sequentially, checking for a match, if it doesn't match it breaks out of the loop immediately without checking further images.
How do I check all of my images in my array for a match and then break out if none are found?
for image in image_list:
found = pyautogui.locateCenterOnScreen(image)
if Found != None:
pyautogui.click(x)
else:
pyautogui.click(y)
break
Full working code updated with details from the comments.
import os
import pyautogui as py
from PIL.ImageOps import grayscale
a = 0
aC = 0
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'):
image_list.append(file)
while True:
if py.locateOnScreen('a.jpg') != None:
breaking_bool = False
while breaking_bool is False:
#Main Find Loop
for image in image_list:
name = image
Found = py.locateCenterOnScreen(image)
if Found != None:
py.moveTo(1700,1000,0.1)
py.sleep(0.01)
py.click()
py.sleep(1)
break
else:
py.moveTo(1415,1000,0.1)
py.sleep(0.01)
py.click()
py.sleep(1)
breaking_bool = True
aC = aC + 1
a = a + 1
This is a case for the for-else
loop.
The else
block is evaluated when the loop runs without encountering a break statement.
for image in image_list:
found = pyautogui.locateCenterOnScreen(image)
if found != None:
pyautogui.click(x)
break
else:
pyautogui.click(y)