简体   繁体   中英

Loop with 'if' and 'else' - alternative to 'break'

I would like to break out of the following code if (for example) leg1_horse1.value==1 were TRUE, the code would 'break' out and continue after the very bottom line of this code.

if leg1_horse1.value==1:
    one=pyautogui.locateOnScreen('1.png')
    pyautogui.moveTo(one,duration=1)
    pyautogui.move(670,0,duration=1)
    pyautogui.click()
else:
    pyautogui.press('down')

if leg1_horse1.value==2:
    two=pyautogui.locateOnScreen('2.png')
    pyautogui.moveTo(two,duration=1)
    pyautogui.move(670,0,duration=1)
    pyautogui.click()
else:
    pyautogui.press('down')

if leg1_horse1.value==3:
    three=pyautogui.locateOnScreen('3.png')
    pyautogui.moveTo(three,duration=1)
    pyautogui.move(670,0,duration=1)
    pyautogui.click()
else:
    pyautogui.press('down')

if leg1_horse1.value==4:
    four=pyautogui.locateOnScreen('4.png')
    pyautogui.moveTo(four,duration=1)
    pyautogui.move(670,0,duration=1)
    pyautogui.click()
else:
    pyautogui.press('down')

if leg1_horse1.value==5:
    five=pyautogui.locateOnScreen('5.png')
    pyautogui.moveTo(five,duration=1)
    pyautogui.move(670,0,duration=1)
    pyautogui.click()
else:
    pyautogui.press('down')

In other words the solution code might look something like this:

if leg1_horse1.value==1:
    one=pyautogui.locateOnScreen('1.png')
    pyautogui.moveTo(one,duration=1)
    pyautogui.move(670,0,duration=1)
    pyautogui.click()
    ***break***
else:
    pyautogui.press('down')

if leg1_horse1.value==2:
    two=pyautogui.locateOnScreen('2.png')
    pyautogui.moveTo(two,duration=1)
    pyautogui.move(670,0,duration=1)
    pyautogui.click()
    ***break***
else:
    pyautogui.press('down')

if leg1_horse1.value==3:
    three=pyautogui.locateOnScreen('3.png')
    pyautogui.moveTo(three,duration=1)
    pyautogui.move(670,0,duration=1)
    pyautogui.click()
    ***break***
else:
    pyautogui.press('down')

if leg1_horse1.value==4:
    four=pyautogui.locateOnScreen('4.png')
    pyautogui.moveTo(four,duration=1)
    pyautogui.move(670,0,duration=1)
    pyautogui.click()
    ***break***
else:
    pyautogui.press('down')

if leg1_horse1.value==5:
    five=pyautogui.locateOnScreen('5.png')
    pyautogui.moveTo(five,duration=1)
    pyautogui.move(670,0,duration=1)
    pyautogui.click()
    ***break***
else:
    pyautogui.press('down')

This doesn't work of course. I feel there is some sort of nested loop that would help here - but I can't quite put my finger on it.

Thankyou in advance.

if leg1_horse1.value==1:
    one=pyautogui.locateOnScreen('1.png')
    pyautogui.moveTo(one,duration=1)
    pyautogui.move(670,0,duration=1)
    pyautogui.click()
elif leg1_horse1.value==2:
    two=pyautogui.locateOnScreen('2.png')
    pyautogui.moveTo(two,duration=1)
    pyautogui.move(670,0,duration=1)
    pyautogui.click()
elif ...==...:
.
.
.
else:
    pyautogui.press('down')

if the first condition matches the other will not execute.

EDIT

Seeing your code there is an even better option I think since all statement looks the same except for one command parameter:

image_map = {1:'1.png', 2:'2.png', ...}

if leg1_horse1.value in image_map.keys():
    one=pyautogui.locateOnScreen(image_map[leg1_horse1.value])
    pyautogui.moveTo(one,duration=1)
    pyautogui.move(670,0,duration=1)
    pyautogui.click()
else:
    pyautogui.press('down')

Using this you will be able to add quickly an option without adding 5 lines each time and it's easier to read

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM