How do I make python to wait for a pressed key


Question

I want my script to wait until the user presses any key.

How do I do that?

1
478
6/10/2016 7:38:48 PM

Accepted Answer

In Python 3, no raw_input() exists. So, just use:

input("Press Enter to continue...")

In Python 2, you should use raw_input(), as input(prompt) is equivalent to eval(raw_input(prompt)):

raw_input("Press Enter to continue...")

This only waits for a user to press enter though, so you 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.

429
3/27/2019 8:01:55 PM

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


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon