我希望脚本等待用户按下任何键。
我怎么做?
#1楼
如果要查看他们是否按下了确切的键(例如说“ b”),请执行以下操作:
while True:
choice = raw_input("> ")
if choice == 'b' :
print "You win"
input("yay")
break
#2楼
在Python 3中,不存在raw_input()
。 因此,只需使用:
input("Press Enter to continue...")
在Python 2中,您应该使用raw_input()
,因为input(prompt)
等效于eval(raw_input(prompt))
:
raw_input("Press Enter to continue...")
但是,这仅等待用户按Enter键,因此您可能要使用msvcrt ((仅适用于Windows / DOS)使用msvcrt模块可以访问Microsoft Visual C / C ++运行时库(MSVCRT)中的许多功能):
import msvcrt as m
def wait():
m.getch()
这应该等待按键。
#3楼
如果可以,请根据系统命令使用以下命令:
Linux:
os.system('read -s -n 1 -p "Press any key to continue..."')
print
视窗:
os.system("pause")
#4楼
os.system似乎总是调用sh,后者无法识别s和n选项以进行读取。 但是,可以将read命令传递给