How do I make my python script wait until the user presses any key?
Advertisement
Answer
In Python 3, use input()
:
JavaScript
x
2
1
input("Press Enter to continue...")
2
In Python 2, use raw_input()
:
JavaScript
1
2
1
raw_input("Press Enter to continue...")
2
This only waits for the user to press enter though.
On Windows/DOS, one might want to use msvcrt
. The msvcrt
module gives you access to a number of functions in the Microsoft Visual C/C++ Runtime Library (MSVCRT):
JavaScript
1
4
1
import msvcrt as m
2
def wait():
3
m.getch()
4
This should wait for a key press.
Notes:
In Python 3, raw_input()
does not exist.
In Python 2, input(prompt)
is equivalent to eval(raw_input(prompt))
.