Ad

I Can't Stop The Loop In Python

- 1 answer

Here is my problem, I'm a beginner and I try to loop a "find and click" on a specific image ( the "NON" button ), it works but the loop don't stop when all the images are clicked.

How can I tell it that when there's no image left to stop the loop and continue with the code?

here's my code:

NON2 = pyautogui.locateCenterOnScreen('NON2.png', grayscale=True, confidence=0.9)

while NON2:
    time.sleep(2)
    click = pyautogui.locateCenterOnScreen('NON2.png', grayscale=True, confidence=0.9)
    pyautogui.click(click)

pyautogui.click(44,965)

and here's the screenshot of the "non" button:

NON button

Ad

Answer

DISCALIMER I don't know pyautogui.

You'll never update the NON2 button in the loop. Try this:

NON2 = pyautogui.locateCenterOnScreen('NON2.png', grayscale=True, confidence=0.9)

while NON2:
    time.sleep(2)
    NON2 = pyautogui.locateCenterOnScreen('NON2.png', grayscale=True, confidence=0.9)
    pyautogui.click(NON2)

pyautogui.click(44,965)
Ad
source: stackoverflow.com
Ad