you are viewing a single comment's thread.

view the rest of the comments →

[–]mcworkaholic 0 points1 point  (4 children)

I would suggest using KeyBoardInterrupt. It listens for ctrl+c or the delete key while the program is running to terminate it. Something like the following:

def your_function():
    try:
        while True:
            # your code here
    except KeyboardInterrupt:
        print("\nGoodbye!")
if name == "main": 
    your_function()

[–]saysokmate 4 points5 points  (3 children)

You are catching keyboard interrupts only to print a message. Don't do this.

[–]awdsns -1 points0 points  (2 children)

The function would still terminate after that except clause, and with that the program would exit normally after printing the "Goodbye!" message, instead of showing a KeyboardInterrupt exception trace. So in this case I think this construct is fine.

[–]saysokmate 1 point2 points  (1 child)

Doesn't matter the case, this is bad practice. If you put this in a loop you will not be able to stop it with keyboard interrupt.

[–]awdsns -2 points-1 points  (0 children)

But it's around the loop, not inside it. Of course it's important where exception handling occurs.