退出功能不起作用

时间:2015-05-14 00:37:10

标签: python exit

如果价值不是我想要的,但我没有做任何事情,我试图退出该计划。什么似乎是问题?我认为它会停止程序,但不打印"无效的子网掩码"。

from sys import exit
def maskvalidation(mask):
    string = mask.split(".")
    toInt = [int(i) for i in string]
    binary = [bin(x) for x in toInt]
    purebin = []
    for x in binary:
        purebin.append(x[2:])
    if purebin[0] == "11111111":
        print("good")
    else:
        exit("Invalid Subnet Mask")

maskvalidation("251.0.0.0")

1 个答案:

答案 0 :(得分:2)

sys.exit

的帮助
    exit(...)
        exit([status])

        Exit the interpreter by raising SystemExit(status).
        If the status is omitted or None, it defaults to zero (i.e., success).
        If the status is an integer, it will be used as the system exit status.
        If it is another kind of object, it will be printed and the system
        exit status will be one (i.e., failure).

因此在问题中正确使用它。

此外,您的示例程序在Linux上适用于Python2.7和Python3.4。

然而它确实将输出发送到stderr而不是stdout,所以也许这就是你看到的问题