python - 如何让程序只有在按下回车键时才继续运行?

标签 python python-3.x

ent = input("Press the Enter key to spin the roulette wheel.")
if ent == "":
    print("Roulette wheel spinning...")
    print("Roulette wheel spinning...")
else:
    #I want it to do nothing till only enter is hit What can I write here?

有没有办法让当按下除 Enter 之外的其他键时,它不会执行任何操作,直到只按下 Enter 为止?使用输入允许用户写一些东西,因此当他们按下回车键时,它总是会运行。如果我可以禁用写入并且仅在按下回车键时才响应,我会更喜欢它。

最佳答案

您可以在输入周围放置一个 while 循环,以便在输入等于 Enter 之前用户无法继续。例如:

while True:
    ent = input("Press the Enter key to spin the roulette wheel.")
    if ent == "":
        break
    print("Roulette wheel spinning...")
    print("Roulette wheel spinning...")

希望这能有所帮助。

关于python - 如何让程序只有在按下回车键时才继续运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54936903/

相关文章:

python - 我如何在 PythonAnywhere 上而不是从 Dropbox 共享文件夹部署 web2py?

Python:合并两个字典

python - 使用 Python 从 REST API 获取大型分页数据

python - 如何获取单个行的值?

python - 为什么有些 Python 变量保持全局,而有些需要定义为全局

python - 从 Voronoi 单元获取有界多边形坐标

python - 通过 Tensorflow 中的段长度计算 tf.math.segment_sum 中所需的段 ID

python - 如何在 Python Django 中从表单实例创建表单集

java - 远程系统事件通知库

Python - 如何将此时间戳格式转换为日期时间?