【问题标题】:How to exit python console if condition does not satisfies如果条件不满足如何退出 python 控制台
【发布时间】:2021-05-15 07:04:22
【问题描述】:

我想输入一个整数作为用户的密码,如果密码不匹配,python 控制台应该关闭。

【问题讨论】:

    标签: python python-3.x python-2.7 if-statement passwords


    【解决方案1】:

    exit() 命令可以提供帮助。参考以下代码:

    password = int(input())
    your_password = 123
    if password!=your_password:   #match the password
        exit()          #exit if password does not match
    print("Welcome")
    

    【讨论】:

      【解决方案2】:

      您可以使用sys 模块中的exit() 功能。

      通常使用非零退出来让服务的调用者知道程序退出失败。

      import sys
      if notValid(password):
          sys.exit(1)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-22
        • 1970-01-01
        • 2020-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多