如何等待按键?

新手上路,请多包涵

如何让我的 python 脚本等到用户按下任何键?

原文由 Janusz 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.4k
2 个回答

Python 3 中,使用 input()

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

Python 2 中,使用 raw_input()

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

这只等待用户按回车键。


在 Windows/DOS 上,可能需要使用 msvcrtmsvcrt 模块使您可以访问 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))

原文由 riza 发布,翻译遵循 CC BY-SA 4.0 许可协议

在 Python 3 中,使用 input()

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

在 Python 2 中,使用 raw_input()

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

原文由 Greg Hewgill 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题