简体   繁体   中英

Traceback accumulation only after Keyboardinterrutps

This is my code:

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()

The strange thing is that the code runs just fine. I only get tracebacks after breaking my loop with a KeyboardInterrupt , spilling out accumulated tracebacks from each loop that ran like this:

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

This specifically happens only if I break the code manually, can anyone shed some light as to what best practice I am ignoring?

If you want to see the output from your print statements immediately, just use flush=True .

Whether output is buffered is usually determined by file , but if the flush keyword argument is true, the stream is forcibly flushed.


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()

Exception tracebacks are only printed when you exit your program with an exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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