I want my python script to wait until the user presses any key.
How do I do that?
Python wait for user input We can use input() function to achieve this. In this case, the program will wait indefinitely for the user input. Once the user provides the input data and presses the enter key, the program will start executing the next statements. sec = input('Let us wait for user input.
If you've got a Python program and you want to make it wait, you can use a simple function like this one: time. sleep(x) where x is the number of seconds that you want your program to wait.
sleep() Arguments. secs - The number of seconds the Python program should pause execution. This argument should be either an int or float.
To detect keypress, we will use the is_pressed() function defined in the keyboard module. The is_pressed() takes a character as input and returns True if the key with the same character is pressed on the keyboard.
In Python 3 use input()
:
input("Press Enter to continue...")
In Python 2 use raw_input()
:
raw_input("Press Enter to continue...")
This only waits for the user to press enter though.
One might want to use msvcrt ((Windows/DOS only) The msvcrt module gives you access to a number of functions in the Microsoft Visual C/C++ Runtime Library (MSVCRT)):
import msvcrt as m def wait(): m.getch()
This should wait for a key press.
Additional info:
in Python 3 raw_input()
does not exist
In Python 2 input(prompt)
is equivalent to eval(raw_input(prompt))
One way to do this in Python 2, is to use raw_input()
:
raw_input("Press Enter to continue...")
In python3 it's just input()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With