使用“if”和“else”循环 - 替代“break”

时间:2021-06-09 14:59:24

标签: python loops while-loop nested pyautogui

如果(例如)leg1_horse1.value==1 为 TRUE,我想中断以下代码,代码将“中断”并在此代码的最底行之后继续。

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')

换句话说,解决方案代码可能如下所示:

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')

这当然行不通。我觉得这里有某种嵌套循环会有所帮助 - 但我无法完全理解它。

先谢谢你。

1 个答案:

答案 0 :(得分:1)

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')

如果第一个条件匹配,另一个将不会执行。

编辑

看到你的代码,我认为有一个更好的选择,因为除了一个命令参数外,所有语句看起来都一样:

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')

使用它您将能够快速添加一个选项,而无需每次添加 5 行并且更易于阅读