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.
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):
import msvcrt as m
def wait():
m.getch()
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)).
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.
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):
import msvcrt as m
def wait():
m.getch()
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)).
In Python 3, use input():
input("Press Enter to continue...")
In Python 2, use raw_input():
raw_input("Press Enter to continue...")
How to 'press any key to continue' in Python 3?
Solved: How to perform a key press 'Enter' in Python? - Autodesk Community
How do I have a "press enter to continue" feature in python? - Stack Overflow
Accepting the 'Return' key on keyboard as user input?
I'm a bit baffled because according to my brief search, there seems to be no general and platform-independent way of waiting for the user to press any key in Python 3:
-
2017-11: https://www.reddit.com/r/learnpython/comments/7bij6z/how_to_press_any_key_to_continue_in_python_34/
-
2009-09: https://stackoverflow.com/questions/1394956/how-to-do-hit-any-key-in-python
-
2009-06: https://stackoverflow.com/questions/983354/how-do-i-make-python-wait-for-a-pressed-key
-
2012-11: https://stackoverflow.com/questions/13207678/whats-the-simplest-way-of-detecting-keyboard-input-in-python-from-the-terminal answer 1 looks rather complicated
-
2019-07: this would suggest an "if windows than m.getch() else input()"-approach. Is this the way to go?
I'd prefer not to use any external dependency for this kind of simple functionality and I'd like to use a (standard) method that works with Windows and non-Windows OS. So far, I got the impression that there is usually "one single Python way of doing basic things".
Thanks for your opinion on this. (I can only test Linux behavior at my side at the moment but my tools are supposed to run on Windows as well.)
I want to start a simple text-based program by having the user press 'Return/Enter' to start. An 'if' loop will initiate when input == *whatever the return key value is**.
I've looked online and found the Unicode value for 'return' in Python. I tried assigning that value to const ENTER and having input == ENTER, but this doesn't work. I don't know how else to use the Unicode value to trigger the 'if' loop, or if I'm even on the right track with that.
Any other info I find is on how to use input() to return a value, how to return a key from a data set, etc. Any help or hints on where to look?
Use this
try:
input= raw_input
except NameError:
pass
If raw_input exists, it will be used for input. If it doesn't exist, input still exists.
you could do something on the line of ...
def myinput(prompt):
try:
return raw_input(prompt)
except NameError:
return input(prompt)
... but don't.
Instead, just use raw_input() on your program, and then use 2to3 to convert the file to python 3.x. That will convert all the raw_input()s for you and also other stuff you might be missing.
That's the recommended way to keep a software working on both python 2 and python 3 and also keep sanity.