需要在Python中的简单子网程序中进行一些错误检查

时间:2013-11-11 19:45:36

标签: python subnet

我有一个子网程序,用于显示所需数量的主机或网络的子网掩码。我的问题是我不能让其他人在底层工作。它一直告诉我,它有一个意想不到的意外,但当我缩进它说意外的unindent。如何解决这个问题,以便它告诉用户唯一接受的输入是“主机”,“网络”或“q”。

 while True:
    print("-------------------------------------------------------------------")
    print("")
    print("Welcome to the Basic Information Generator : Automatic Subnet System")
    print("")
    try:
        inquire = raw_input("Are you figuring for HOSTS or NETWORKS? \
        ('Q' exits') :").lower()
        if inquire == 'hosts':
            try:
                number = int(raw_input("How many HOSTS do you need on the network?"))
                if number == 0:
                    print("[WARNING] Your subnet mask is 255.255.255.254 [WARNING]")
                    print("This subnet has NO usable hosts...")
                if number <= 2 and number > 0:
                    print("Your subnet mask is 255.255.255.252")
                if number <= 6 and number > 2:
                    print("Your subnet mask is 255.255.255.248")
                if number <= 14 and number > 6:
                    print("Your subnet mask is 255.255.255.240")
                if number <= 30 and number > 14:
                    print("Your subnet mask is 255.255.255.224")

其余的这些在pyscripter中正确缩进,但不会在这里缩进......

            if number <= 62 and number > 30:
                print("Your subnet mask is 255.255.255.192")
            if number <= 126 and number > 62:
                print("Your subnet mask is 255.255.255.128")
            if number <= 254 and number > 126:
                print("Your subnet mask is 255.255.255.0")
            if number <= 510 and number > 254:
                print("Your subnet mask is 255.255.254.0")
            if number <= 1022 and number > 510:
                print("Your subnet mask is 255.255.252.0")
            if number <= 2046 and number > 1022:
                print("Your subnet mask is 255.255.248.0")
            if number <= 4094 and number > 2046:
                print("Your subnet mask is 255.255.240.0")
            if number <= 8190 and number > 4094:
                print("Your subnet mask is 255.255.224.0")
            if number <= 16382 and number > 8190:
                print("Your subnet mask is 255.255.192.0")
            if number <= 32766 and number > 16382:
                print("Your subnet mask is 255.255.128.0")
            if number <= 65534 and number > 32766:
                print("Your subnet mask is 255.255.0.0")
            if number <= 131070 and number > 65534:
                print("Your subnet mask is 255.254.0.0")
            if number <= 262142 and number > 131070:
                print("Your subent mask is 255.252.0.0")
            if number <= 524286 and number > 262142:
                print("Your subnet mask is 255.248.0.0")
            if number <= 1048574 and number > 524286:
                print("Your subnet mask is 255.240.0.0")
            if number <= 2097150 and number > 1048574:
                print("Your subnet mask is 255.224.0.0")
            if number <= 4194302 and number > 2097150:
                print("Your subnet mask is 255.192.0.0")
            if number <= 8388606 and number > 4194302:
                print("Your subnet mask is 255.128.0.0")
            if number <= 16777214 and number > 8388606:
                print("Your subnet mask is 255.0.0.0")
            if number <= 33554430 and number > 16777214:
                print("Your subnet mask is 254.0.0.0")
            if number <= 67108862 and number > 33554430:
                print("Your subnet mask is 252.0.0.0")
            if number <= 134217726 and number > 67108862:
                print("Your subnet mask is 248.0.0.0")
            if number <= 268435454 and number > 134217726:
                print("Your subnet mask is 240.0.0.0")
            if number <= 536870910 and number > 268435454:
                print("Your subnet mask is 224.0.0.0")
            if number <= 1073741822 and number > 536870910:
                print("Your subnet mask is 192.0.0.0")
            if number <= 2147483646 and number > 1073741822:
                print("Your subnet mask is 128.0.0.0")
            if number > 2147483646:
                print("Invalid Number of Hosts. No such Netmask applicable.")
        except:
                print("Enter a number between 1 and 2,147,483,646")
    elif inquire == 'networks':
        number = input("How many SUBNETS do you need on the network?")
        if number == 0:
            print("[WARNING] Your subnet mask is 0.0.0.0 [WARNING]")
            print("This netmask yeilds NO usable subnets...")
        if number == 1:
            print("Your subnet mask is 255.0.0.0")
        if number == 2:
            print("Your subnet mask is 255.128.0.0")
        if number <= 4 and number > 2:
            print("Your subnet mask is 255.192.0.0")
        if number <= 8 and number > 4:
            print("Your subnet mask is 255.224.0.0")
        if number <= 16 and number > 8:
            print("Your subnet mask is 255.240.0.0")
        if number <= 32 and number > 16:
            print("Your subnet mask is 255.248.0.0")
        if number <= 64 and number > 32:
            print("Your subnet mask is 255.252.0.0")
        if number <= 128 and number > 64:
            print("Your subnet mask is 255.254.0.0")
        if number <= 256 and number > 128:
            print("Your subnet mask is 255.255.0.0")
        if number <= 512 and number > 256:
            print("Your subnet mask is 255.255.128.0")
        if number <= 1024 and number > 512:
            print("Your subnet mask is 255.255.192.0")
        if number <= 2048 and number > 1024:
            print("Your subnet mask is 255.255.224.0")
        if number <= 4096 and number > 2048:
            print("Your subnet mask is 255.255.240.0")
        if number <= 8192 and number > 4096:
            print("Your subnet mask is 255.255.248.0")
        if number <= 16384 and number > 8192:
            print("Your subnet mask is 255.255.252.0")
        if number <= 32768 and number > 16384:
            print("Your subnet mask is 255.255.254.0")
        if number <= 65536 and number > 32768:
            print("Your subnet mask is 255.255.255.0")
        if number <= 131072 and number > 65536:
            print("Your subnet mask is 255.255.255.128")
        if number <= 262144 and number > 131072:
            print("Your subnet mask is 255.255.255.192")
        if number <= 524288 and number > 262144:
            print("Your subnet mask is 255.255.255.224")
        if number <= 1048576 and number > 524288:
            print("Your subnet mask is 255.255.255.240")
        if number <= 2097152 and number > 1048576:
            print("Your subnet mask is 255.255.255.248")
        if number <= 4194304 and number > 2097152:
            print("Your subnet mask is 255.255.255.252")
    elif inquire == 'q':
            break
    else:
        print("NEED CERTAIN THINGS")

“需要某些东西”只是填充码,所以我会知道'else:'声明有效......

1 个答案:

答案 0 :(得分:1)

看起来你在街区尽头错过了except:

    try:
        inquire = ...                         
        ...
        if:
            ...
        else:
        print("NEED CERTAIN THINGS")    # unexpected unindent error on this line...
     except:     # Because this is missing
        pass     # Do something here

不一定是except:,你可以使用try...finallytry...except...else,但你确实需要一些东西。 Exception handling.