【问题标题】:Loop with 'if' and 'else' - alternative to 'break'使用 'if' 和 'else' 循环 - 替代 'break'
【发布时间】: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


【解决方案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 行,并且更易于阅读

【讨论】:

  • 谢谢你......如果有多匹马怎么办 - 我想在任何一匹马上突围?........我已经更新了这个问题以更清楚
  • @GDog 那么你会有多个 elif 块。看来您需要重做有关 if..elif 块的教程。这是一个不错的:tutorialspoint.com/python/python_if_else.htm
  • 您可以根据需要堆叠任意数量的 elif,如果您的 else 语句在每个条件下都保持不变,那么您的 else 语句就可以了
猜你喜欢
  • 2014-03-08
  • 2014-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-07
  • 1970-01-01
  • 2019-01-23
相关资源
最近更新 更多