【发布时间】:2021-08-26 14:41:24
【问题描述】:
如果(例如)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')
这当然行不通。我觉得这里有某种嵌套循环会有所帮助 - 但我不能完全确定它。
提前谢谢你。
【问题讨论】:
-
您正在寻找
if..elif..else。 -
这不是一个循环。循环是运行(可能)多次迭代的块。
标签: python loops while-loop nested pyautogui