我的代码必须返回一个有效的网络掩码。它必须检查二进制表示是True
还是False
。
在我的代码中有问题。当我检查1
它时说第一个是真的,第二个是假的......所有都1
必须返回True
,所有 0 都必须返回False
。
def is_valid_netmask(numberlist):
ips = numberlist
result = True
#for ip in ips:
#num = int(ip)
#if not (num >= 0 and num <= 255):
#result = False
if len(ips) != 4:
return False
if result == False:
print("not valid")
else:
print("valid")
octet = ips
octet_bin = [format(int(i), '08b') for i in octet]
binary_netmask = ("").join(octet_bin)
print(binary_netmask)
checking_ones = True
for symbol in binary_netmask:
print("the current symbol is ", symbol)
print(f"I only encountered one so far: {checking_ones}")
if checking_ones and symbol == "0":
print("so i know that the netmask is not valid")
return False
elif symbol == "1":
checking_ones = False
print("I'm done so I know the netmask is valid")
return True
输出
valid
11111111111111111111111100000000
the current symbol is 1
I only encountered one so far: True #correct
the current symbol is 1
I only encountered one so far: False #not correct
the current symbol is 1
I only encountered one so far: False #not correct
the current symbol is 1
I only encountered one so far: False #not correct
the current symbol is 1
I only encountered one so far: False #not correct
the current symbol is 1
I only encountered one so far: False #not correct
the current symbol is 1
I only encountered one so far: False #not correct
the current symbol is 1
I only encountered one so far: False #not correct
the current symbol is 1
I only encountered one so far: False #not correct
the current symbol is 1
I only encountered one so far: False #not correct
the current symbol is 1
I only encountered one so far: False #not correct
the current symbol is 1
I only encountered one so far: False #not correct
the current symbol is 1
I only encountered one so far: False #not correct
the current symbol is 1
I only encountered one so far: False #not correct
the current symbol is 1
I only encountered one so far: False #not correct
the current symbol is 1
I only encountered one so far: False #not correct
the current symbol is 1
I only encountered one so far: False #not correct
the current symbol is 1
I only encountered one so far: False #not correct
the current symbol is 1
I only encountered one so far: False #not correct
the current symbol is 1
I only encountered one so far: False #not correct
the current symbol is 1
I only encountered one so far: False #not correct
the current symbol is 1
I only encountered one so far: False #not correct
the current symbol is 1
I only encountered one so far: False #not correct
the current symbol is 1
I only encountered one so far: False #notcorrect
the current symbol is 0
I only encountered one so far: False #correct
the current symbol is 0
I only encountered one so far: False #correct
the current symbol is 0
I only encountered one so far: False #correct
the current symbol is 0
I only encountered one so far: False #correct
the current symbol is 0
I only encountered one so far: False #correct
the current symbol is 0
I only encountered one so far: False #correct
the current symbol is 0
I only encountered one so far: False #correct
the current symbol is 0
I only encountered one so far: False #correct
I'm done so I know the netmask is valid
True