python - 如何使用回车键退出程序

标签 python

Michael Dawson 在他的书 Python 编程(第三版,第 14 页)中说,如果我输入 input("\n\nPress the Enter key to exit.") 当用户按下 Enter 键时程序将结束。

我已经尝试过多次,但没有发生。我尝试过使用Python 3.1和3.3。如有帮助,我们将不胜感激。

最佳答案

这是一个相当简单的概念,请看一下这个示例

x = input("Hit Enter to Exit, or Input any other key to continue ")

if not x :
    print("Exiting the Program.")
    exit()

else:
    a = int(input("\nEnter a Number : "))
    b = int(input("Enter another Number : "))
    print("Sum of the two numbers : ", a+b)

关于python - 如何使用回车键退出程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21759946/

相关文章:

python - Ubuntu : Unable to correct problems, 你有损坏的包

python - 在 SQLite 中插入日期

python - 如何通过用连接路径起点和终点的边替换每条最长的非分支路径来减少 DAG?

python - 如何在协方差矩阵中找到退化的行/列

python - wx.ProgressDialog 太小

python - 将 SConstruct 代码划分为一组别名;默认情况下不调用任何代码

python - 仅 os.walk .jpg

python - CountVectorizer fit-transform() 不适用于自定义 token_pattern

python - 如何在 Python + Tkinter 中获取菜单复选按钮来调用函数?

python - 批量加载巨大的数据集来训练 pytorch