FeerQ
FeerQ

Reputation: 11

sys.exit function doesn't end the program

there's my code:

import sys
import keyboard
import rotatescreen
from time import sleep
pd = rotatescreen.get_primary_display()
angel = [90, 180, 270, 0]
for i in range(2):
    for x in angel:
        pd.rotate_to(x)
        sleep(0.5)
if keyboard.is_pressed("p"):
    sys.exit(0)

So the program is working very well, doing his task, but the last 2 lines just don't seem to work. When i press 'p' the program just finishes his work instead of stopping.

Upvotes: 1

Views: 279

Answers (1)

Yaakov Bressler
Yaakov Bressler

Reputation: 12158

You want to listen for the keyboard event as close to the "execution" of your operation as possible. In your case, that's in the nested loop:

for i in range(2):
    for x in angel:
        if keyboard.is_pressed("p"):
            sys.exit(0)
        pd.rotate_to(x)
        sleep(0.5)

I would also give the documentation a read through, specifically this: Common patterns and mistakes

Upvotes: 1

Related Questions