简体   繁体   English

当 while 循环的条件变为 False 时,跳出嵌套在 while 循环中的 for 循环

[英]Break out of a for loop nested in a while loop when while loop's condition becomes False

I have a while loop that iterates for 8 seconds, and inside it is a for loop which iterates 4 times, each time executing some pyautogui press functions.我有一个迭代 8 秒的 while 循环,在它里面是一个迭代 4 次的 for 循环,每次执行一些 pyautogui press 函数。 However, when my loop breaks, the for loop within continues until completion.但是,当我的循环中断时,内部的 for 循环会继续直到完成。 Code:代码:

import time
import pyautogui

timeout = time.time() + 8


while time.time() < timeout:
    for i in range(4):
        pyautogui.press("1")
        pyautogui.press("1")
        pyautogui.press("1")
        pyautogui.press("1")
        pyautogui.press("1")
        pyautogui.press("1")
        pyautogui.press("1")
        pyautogui.press("1")
        pyautogui.press("1")
        pyautogui.press("1")
        pyautogui.press("4")
        pyautogui.press("enter")
print(f"{time.time() - timeout } seconds more than while loop execute time")

Print statement output:打印报表output:

2.4774444103240967 seconds more than while loop execute time

So, how can I get my for loop to end with my while loop?那么,我怎样才能让我的 for 循环以我的 while 循环结束呢? I checked around on stack overflow but found nothing.我检查了堆栈溢出但一无所获。

(used pyautogui.press("") cause I got my error using them and they execute slower than print statements (can see the time difference better)) (使用pyautogui.press("")因为我在使用它们时出错并且它们执行得比打印语句慢(可以更好地看到时差))

Well, your description doesn't reflect reality.好吧,你的描述并不能反映现实。 The "for" loop certainly will end with the "while" loop, but you can't interrupt the "for" loop in the middle. “for”循环肯定会以“while”循环结束,但是你不能在中间打断“for”循环。 If you want to stop in mid-keystroke, then you'll need to check the time at every keystroke:如果你想在击键中途停止,那么你需要在每次击键时检查时间:

import time
import pyautogui

timeout = time.time() + 8
msg = "11111111114*" * 4

for c in msg:
    if time.time() >= timeout:
        break
    if c == '*':
        c = 'enter'
    pyautogui.press(c)
print(f"{time.time() - timeout } seconds more than while loop execute time")

Alternatively:或者:

import time
import pyautogui

timeout = time.time() + 8
msg = (list("11111111114")+['enter']) * 4

while time.time() < timeout:
    if msg:
        pyautogui.press(msg.pop(0))
    else:
        time.sleep(0.1)

print(f"{time.time() - timeout } seconds more than while loop execute time")

You can replace the body of the while loop with a call to next on an appropriately defined generator.您可以在适当定义的生成器上用对next的调用替换while循环的主体。 In some sense, this is a primitive implementation of a thread;从某种意义上说,这是线程的原始实现; execution of the generator is interleaved with the repeated evaluations of the timeout condition.生成器的执行与超时条件的重复评估交织在一起。 (In practice, this may only be useful as an example of how threading works.) (在实践中,这可能仅作为线程如何工作的示例有用。)

def my_generator():
    for i in range(4):
        yield pyautogui.press("1")
        yield pyautogui.press("1")
        yield pyautogui.press("1")
        yield pyautogui.press("1")
        yield pyautogui.press("1")
        yield pyautogui.press("1")
        yield pyautogui.press("1")
        yield pyautogui.press("1")
        yield pyautogui.press("1")
        yield pyautogui.press("1")
        yield pyautogui.press("4")
        yield pyautogui.press("enter") 
    
foo = my_generator()

while time.time() < timeout:
    next(foo)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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