【问题标题】:Exit shutdown function python [duplicate]退出关闭功能python [重复]
【发布时间】:2013-04-28 15:12:16
【问题描述】:

我编写了以下代码,如果 else 语句为真,我希望程序关闭。我是 python 新手,遇到了一些麻烦

password = raw_input('Enter yes to continue:')
if password == 'yes':
    print'You have chosen to continue' 
else:
    print'You have chosen to exit'

【问题讨论】:

    标签: exit shutdown


    【解决方案1】:

    将此添加到您的代码中

    import sys # This goes at the header
    ...
    if password == 'yes'
        pass # Your code goes on... I put the 'pass' just as a place holder
    else
        sys.exit()
    

    【讨论】:

    • 我的代码现在看起来像这样 import sys password = raw_input('Enter yes to continue:') # 确定是否输入了 yes 或输入了任何内容。 if password == 'yes': print'You have selected to contiue' else: print'You have selected to exit' sys.exit 我希望 python 程序关闭或至少关闭交互式窗口,但我没有得到结果,请帮助,我错过了什么吗?
    • “关机”是指退出程序吗?还是你的意思是别的?
    • @MarkKearney 我纠正了一个错字...而不是sys.exit必须sys.exit()
    • 退出程序还是有关闭python函数?
    • @MarkKearney 只是sys.exit()。它结束了python程序。如果您从控制台(或 Windows 中的命令窗口)调用程序,它会停止执行并退出程序,并返回到系统提示符。
    【解决方案2】:
    import sys
    
    if condition is true:
        pass
        # or your operating row.
    else:
        sys.exit()
    

    【讨论】:

      猜你喜欢
      • 2011-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-22
      • 2018-11-12
      • 1970-01-01
      • 2013-04-14
      相关资源
      最近更新 更多