如何让python等待按下的键?
我希望脚本等待用户按下任何键。
我怎么做?
-
在Python 3中使用
input()
:input("Press Enter to continue...")
在Python 2中使用
raw_input()
:raw_input("Press Enter to continue...")
不过,这仅等待用户按下Enter键。
可能要使用msvcrt((仅适用于
Windows / DOS
)使用msvcrt
模块可以访问Microsoft Visual C / C ++
运行时库(MSVCRT)中的许多功能):import msvcrt as m def wait(): m.getch()
这应该等待按键。
附加信息:
Python 3
raw_input()
中不存在在Python 2
input(prompt)
中等效于eval(raw_input(prompt))
-
在Python 2中执行此操作的一种方法是使用
raw_input()
:raw_input("Press Enter to continue...")
在python3中
input()