繁体   English   中英

仅在Keyboardinterrutps之后累积回溯

[英]Traceback accumulation only after Keyboardinterrutps

这是我的代码:

import autopy
import time
import math
width, height = 400, 400
a, b = 200, 200
r = 150
def drawCicle():
    for x in range(0, 3):
        for angle in range(0, 360, 1):
            x = r * math.sin(math.radians(angle)) + a
            y = r * math.cos(math.radians(angle)) + b
            autopy.mouse.move(int(x),int(y))
            time.sleep(0.002)
def mouseMove():
    counter = 0
    while counter < 4:
        drawCicle()
        counter += 1
    else:
        print('Drawing ' + str(counter) + ' circles')
        print('moving once more in 10 seconds...')
        counter = 0
        time.sleep(10)
        mouseMove()

if __name__ == "__main__":
    mouseMove()

奇怪的是,代码运行得很好。 只有在使用KeyboardInterrupt中断循环后,我才会获得回溯,这样从每个循环中溢出累积的回溯,如下所示:

Traceback (most recent call last):
  File "test.py", line 27, in <module>
    mouseMove()
  File "test.py", line 24, in mouseMove
    mouseMove()
  File "test.py", line 24, in mouseMove
    mouseMove()
  File "test.py", line 24, in mouseMove
    mouseMove()
  File "test.py", line 23, in mouseMove
    time.sleep(10)
KeyboardInterrupt

仅当我手动破坏代码时,这种情况才会发生,任何人都可以阐明我所忽略的最佳实践吗?

如果要立即查看打印语句的输出,请使用flush=True

输出是否被缓冲通常由file决定,但是如果flush关键字参数为true,则将强制刷新流。


def mouseMove():
    counter = 0
    while counter < 4:
        drawCicle()
        counter += 1
    else:
        print('Drawing ' + str(counter) + ' circles', flush=True)
        print('moving once more in 10 seconds...', flush=True)
        counter = 0
        time.sleep(10)
        mouseMove()

仅当您退出程序并出现异常时,才打印异常回溯。

暂无
暂无

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

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