WeirdNightowl
WeirdNightowl

Reputation: 11

While loop for if, elif,

What I have here is a part of an automation program for clicking on the screen with certain conditions. I want this block of code to repeat over and over and stop when it clicks at the position with coordinate (867,647), which is when it satisfies every if/elif statement. I tried to put it inside the while loop but end up not working, but even if it works, I think it will repeat infinitely many times and I don't know how to stop it when it satisfies my condition. Can you guys show me how should i put this inside the while loop to function like I want? Thank you guys very much!

if ((pyautogui.pixel(362,349)[0] == 76)  and (pyautogui.pixel(362,349)[1] == 145) and (pyautogui.pixel(362,349)[2] == 186) ):
    click (362,349)
    time.sleep(0.1) 
    click (867,647)
elif ((pyautogui.pixel(1025,342)[0] == 17)  and (pyautogui.pixel(1025,342)[1] == 129) and (pyautogui.pixel(1025,342)[2] == 181) ):
    click (1025,342)
    time.sleep(0.1) 
    click (867,647)    
elif ((pyautogui.pixel(682,347)[0] == 15)  and (pyautogui.pixel(682,347)[1] == 117) and (pyautogui.pixel(682,347)[2] == 169) ):
    click (682,347)
    time.sleep(0.1) 
    click (867,647)    
else:
    click(1026,649)    
  

Upvotes: 1

Views: 131

Answers (1)

Lake
Lake

Reputation: 11

You could add break after click (867,647). I created an example code but couldn't test it. You can try the code below:

while True:
    if ((pyautogui.pixel(362,349)[0] == 76)  and (pyautogui.pixel(362,349)[1] == 145) and (pyautogui.pixel(362,349)[2] == 186) ):
        click (362,349)
        time.sleep(0.1) 
        click (867,647)
        break
    elif ((pyautogui.pixel(1025,342)[0] == 17)  and (pyautogui.pixel(1025,342)[1] == 129) and (pyautogui.pixel(1025,342)[2] == 181) ):
        click (1025,342)
        time.sleep(0.1) 
        click (867,647)
        break
    elif ((pyautogui.pixel(682,347)[0] == 15)  and (pyautogui.pixel(682,347)[1] == 117) and (pyautogui.pixel(682,347)[2] == 169) ):
        click (682,347)
        time.sleep(0.1) 
        click (867,647)    
        break
    else:
        click(1026,649) 
    sleep(1)

Upvotes: 1

Related Questions