【发布时间】:2020-12-14 09:02:35
【问题描述】:
我的代码必须返回一个有效的网络掩码。它必须检查二进制表示是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
【问题讨论】:
-
如果符号为“1”,则将
checking_ones设置为False。问题可能来自那里。