Get control + c input from Python and end the program

Asked 1 years ago, Updated 1 years ago, 119 views

The program you are writing with Python contains a while statement

I want to end the program when the user enters the control + c command when the program is running and when the while turns How do I write the code?

python python3.6

2022-09-21 23:05

1 Answers

You can take the keyboard interrupt exception and process it.

try:
    # operations
except KeyboardInterrupt:
    # Exception when Ctrl+C is entered
    sys.exit() #Exit


2022-09-21 23:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.