So, as the title says, I want a proper code to close my python script.
So far, I’ve used input('Press Any Key To Exit'), but what that does, is generate an error.
I want a code that closes your script without using an error.
Does anyone have an idea? Google gives me the input option, but I don’t want that
It closes using this error:
Traceback (most recent call last):
File "C:/Python27/test", line 1, in <module>
input('Press Any Key To Exit')
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
This syntax error is caused by using
inputon Python 2, which will try toevalwhatever is typed in at the terminal prompt. If you’ve pressed enter then Python will essentially try to eval an empty string,eval(""), which causes aSyntaxErrorinstead of the usualNameError.If you’re happy for "any" key to be the enter key, then you can simply swap it out for
raw_inputinstead:Note that on Python 3
raw_inputwas renamed toinput.For users finding this question in search, who really want to be able to press any key to exit a prompt and not be restricted to using enter, you may consider to use a 3rd-party library for a cross-platform solution. I recommend the helper library
readcharwhich can be installed withpip install readchar. It works on Linux, macOS, and Windows and on either Python 2 or Python 3.