我的无限循环不是无限的 – Python

huangapple go评论153阅读模式
英文:

My infinite loop is not infinite - Python

问题

以下是代码的翻译部分:

import time
import pyautogui

time.sleep(5)

while True:
    time.sleep(5)

    pyautogui.moveTo(455,620)
    pyautogui.click()

    time.sleep(5)

    pyautogui.moveTo(890,15)
    pyautogui.click()
    
    if KeyboardInterrupt:
        break
    else:
        continue

我想要自动化不断打开和关闭 Chrome 选项卡。

我尝试了一个无限循环。

英文:
import time
import pyautogui

time.sleep(5)

while True:
    time.sleep(5)

    pyautogui.moveTo(455,620)
    pyautogui.click()

    time.sleep(5)

    pyautogui.moveTo(890,15)
    pyautogui.click()
    
    if KeyboardInterrupt:
        break
    else:
        continue

I wanted to automate opening and closing a chrome tab endlessly

I tried a while true loop

答案1

得分: 1

KeyboardInterrupt是一个异常,详见https://docs.python.org/3/library/exceptions.html

在这里它说:
"当用户按下中断键(通常是Control-C或Delete键)时引发。"

如果您想捕获它,您需要使用try/except,如下所示:

try:
    while True:
        ...
except KeyboardInterrupt as e:
    ... # 不要在这里使用break,因为您已经离开了循环

然而,根据您的用例,这可能不建议。一旦KeyboardInterrupt被设置,程序继续运行,这可能会导致后续行为变得不可预测。例如,

考虑以下示例:

try:
    while True:
        print(1)
except KeyboardInterrupt as e:
        print(2)
while True:
    try:
        print(3)
    except KeyboardInterrupt as e:
        print(4)
        break

在中断被触发之前会一直打印1,然后打印一个2,然后在下次中断被触发之前会一直打印3,然后打印一个4并退出。

现在同样的情况,但加入了一个额外的if条件:

try:
    while True:
        print(1)
except KeyboardInterrupt as e:
        print(2)
while True:
    try:
        print(3)
        if KeyboardInterrupt:
            print(5)
            break
    except KeyboardInterrupt as e:
        print(4)
        break

与在第二个循环中打印大量的3不同,在这个情况下,它会触发询问KeyboardInterrupt状态的if条件,因为它被设置了,所以立即中断(而不需要按任何键)。

可以在try/except块中持续使用它,但是(根据文档):

**注意:**捕获KeyboardInterrupt需要特殊考虑。因为它可以在不可预测的时刻引发,所以在某些情况下,它可能会使正在运行的程序处于不一致的状态。通常最好允许KeyboardInterrupt尽快结束程序,或者完全避免引发它。(请参阅关于信号处理程序和异常的注意事项。)

英文:

KeyboardInterrupt is an Exception, see https://docs.python.org/3/library/exceptions.html

There it says:
"Raised when the user hits the interrupt key (normally Control-C or Delete)."

If you want to catch it, you'll need to use try/except like so:

try:
    while True:
        ...
except KeyboardInterrupt as e:
    ... # don't use break here, as you've already left the loop

However, depending on your use-case this is likely not recommendable. Once KeyboardInterrupt is set, and the program continues to run, this may/will create unpredictable execution behaviour further down the line. For instance,

Consider these examples:

try:
    while True:
        print(1)
except KeyboardInterrupt as e:
        print(2)
while True:
    try:
        print(3)
    except KeyboardInterrupt as e:
        print(4)
        break

Prints ones until interrupt is hit, then a single 2, then threes until next interrupt hit, then a single 4 and exits.

and now the same, but with an additional if-clause

try:
    while True:
        print(1)
except KeyboardInterrupt as e:
        print(2)
while True:
    try:
        print(3)
        if KeyboardInterrupt:
            print(5)
            break
    except KeyboardInterrupt as e:
        print(4)
        break

Instead of printing lots of threes in the second loop, it hits the if-clause asking about the state of KeyboardInterrupt and since this is set, it breaks immediately (without the pressing of any key).

You could use it continuously within try/except blocks, but (from the docs):

> Note: Catching a KeyboardInterrupt requires special consideration. Because
> it can be raised at unpredictable points, it may, in some
> circumstances, leave the running program in an inconsistent state. It
> is generally best to allow KeyboardInterrupt to end the program as
> quickly as possible or avoid raising it entirely. (See Note on Signal
> Handlers and Exceptions.)

答案2

得分: 1

KeyboardInterrupt是一个异常所以你的代码应该是

import time
import pyautogui

time.sleep(5)

while True:
    try:
        time.sleep(5)

        pyautogui.moveTo(455,620)
        pyautogui.click()

        time.sleep(5)

        pyautogui.moveTo(890,15)
        pyautogui.click()
    
    except KeyboardInterrupt: break

另外,为了使KeyboardInterrupt正常工作,你需要在控制台中直接按下ctrl+c,所以在这一刻控制台窗口必须处于活动状态。

英文:

KeyboardInterrupt is an exception, so your code should be:

import time
import pyautogui

time.sleep(5)

while True:
    try:
        time.sleep(5)

        pyautogui.moveTo(455,620)
        pyautogui.click()

        time.sleep(5)

        pyautogui.moveTo(890,15)
        pyautogui.click()
    
    except KeyboardInterrupt: break

In addition, for KeyboardInterrupt to work, you need to press ctrl+c directly in the console, so the console window must be active at this moment

huangapple
  • 本文由 发表于 2023年5月29日 15:42:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355492.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定