Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a python script wait for a pressed key?

I want my python script to wait until the user presses any key.
How do I do that?

like image 780
Janusz Avatar asked Jun 11 '09 20:06

Janusz


People also ask

How do you make a Python program wait for input?

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.

How do you wait for 5 seconds in Python?

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.

How do you wait seconds in Python?

sleep() Arguments. secs - The number of seconds the Python program should pause execution. This argument should be either an int or float.

How does Python detect key press?

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.


2 Answers

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))

like image 153
riza Avatar answered Sep 24 '22 02:09

riza


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()

like image 41
Greg Hewgill Avatar answered Sep 22 '22 02:09

Greg Hewgill