[英]Simple IF statement in Python 2.7
I need help with the if
statement.The syntax seems to be correct and have verified within this forum as well but not sure why is it going inside the "invalid IF statement". 我需要
if
语句的帮助。语法似乎是正确的,并且也在此论坛中进行了验证,但不确定为什么它会出现在“无效的IF语句”中。
ips=['20.34.178.250']
for index, item in enumerate(ips):
flag1 = 0
host_bytes= item.split('.')
print item,index
for index1, item1 in enumerate(host_bytes):
print item1
if [item1 > 255 and flag1==0]:
print "IP Address is invalid",item1,flag1
flag1=1
print flag1
Output : 输出:
20.34.178.250 0
20
IP Address is invalid 20 0
34
IP Address is invalid 34 1
178
IP Address is invalid 178 1
250
IP Address is invalid 250 1
1
if [item1 > 255 and flag1==0]:
your condition creates a 1-element list
containing True
or False
, which is seen as "truthy" since the list isn't empty, so condition is always True
. 您的条件会创建一个包含
True
或False
的1元素list
,由于列表不为空,因此它被视为“真实的”,因此condition始终为True
。 You mean: 你的意思是:
if (item1 > 255 and flag1==0):
but in python you don't need the parentheses so save some bytes and risk of typos: 但在python中,您不需要括号,因此可以节省一些字节和错别字的风险:
if item1 > 255 and flag1==0:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.