Python 如何使用while循环计算IP地址中第一段的长度
我的代码如下,但不起作用:Python 如何使用while循环计算IP地址中第一段的长度,python,python-3.x,Python,Python 3.x,我的代码如下,但不起作用: IP_address = "216.3.122.18" seg_1_len = 0 for char in IP_address: while char != ".": seg_1_len += 1 print(seg_1_len) if char == '.': break 为什么在for循环中运行while循环。char变量已
IP_address = "216.3.122.18"
seg_1_len = 0
for char in IP_address:
while char != ".":
seg_1_len += 1
print(seg_1_len)
if char == '.':
break
为什么在for循环中运行while循环。char变量已经在每次迭代中存储IP地址的每个字符。所以只要检查char是否为,然后停止,否则在每次迭代中将seg_1_len增加1。如果您像以前一样使用while循环,它将在第一次迭代中转到无限循环。想想看 试试这个:
IP_address = "216.3.122.18"
seg_1_len = 0
for char in IP_address:
if char == '.':
break
else:
seg_1_len+=1
print seg_1_len
输出:
3
根据您的问题,如果仍要使用while循环,请执行以下操作:
IP_address = "216.3.122.18"
seg_1_len = 0
i=0
while IP_address[i] != ".":
seg_1_len += 1
i+=1
print(seg_1_len)
或者如果你想喝点什么,D
为什么在for循环中运行while循环。char变量已经在每次迭代中存储IP地址的每个字符。所以只要检查char是否为,然后停止,否则在每次迭代中将seg_1_len增加1。如果您像以前一样使用while循环,它将在第一次迭代中转到无限循环。想想看 试试这个:
IP_address = "216.3.122.18"
seg_1_len = 0
for char in IP_address:
if char == '.':
break
else:
seg_1_len+=1
print seg_1_len
输出:
3
根据您的问题,如果仍要使用while循环,请执行以下操作:
IP_address = "216.3.122.18"
seg_1_len = 0
i=0
while IP_address[i] != ".":
seg_1_len += 1
i+=1
print(seg_1_len)
或者如果你想喝点什么,D
您使用for循环遍历ip地址,然后使用while循环不断地查询第一个字符,如果while更改为if,那么您的代码可以工作
IP_address = "216.3.122.18"
seg_1_len = 0
for char in IP_address:
if char != ".":
seg_1_len += 1
else:
break
print(seg_1_len)
您使用for循环遍历ip地址,然后使用while循环不断地查询第一个字符,如果while更改为if,那么您的代码可以工作
IP_address = "216.3.122.18"
seg_1_len = 0
for char in IP_address:
if char != ".":
seg_1_len += 1
else:
break
print(seg_1_len)
在用于查找某物的for循环中,通常会使用else,如下面的人为示例:
def seg1len(ipaddy):
seg1 = ""
for char in ipaddy:
if char == '.': # test for equality to the break condition..
break # .. and break at the top of the for loop
seg1 += char # do the for-loop work (you don't need an else clause to the if)
else: # the else would go with the for and would be executed whenever the for loop ran without hitting break
raise ValueError("ipaddy contains no dots")
return len(seg1)
同样,这是一个人为的例子。您应该使用以下其中一种:
IP_address = "216.3.122.18"
seg_1_len = IP_address.find('.') # returns -1 if there is no '.'
或
或
使用while的解决方案可以非常简单:
seg_1_length = 1
while IP_address[seg_1_length] != '.':
seg_1_length += 1
假设我们知道输入是有效的ip4地址,它必须至少包含一个“.”,并且不能以“.”开头
如果您不能假定输入是有效的,则需要更具防御性地编写代码,例如:
i = 0
while i < len(ipaddy) and ipaddy[i] != '.':
i += 1
assert i > 0, "ip address can't start with a dot"
assert i <= 3, "the first octet can't be more than 3 characters long"
seg_1_length = i
在用于查找某物的for循环中,通常会使用else,如下面的人为示例:
def seg1len(ipaddy):
seg1 = ""
for char in ipaddy:
if char == '.': # test for equality to the break condition..
break # .. and break at the top of the for loop
seg1 += char # do the for-loop work (you don't need an else clause to the if)
else: # the else would go with the for and would be executed whenever the for loop ran without hitting break
raise ValueError("ipaddy contains no dots")
return len(seg1)
同样,这是一个人为的例子。您应该使用以下其中一种:
IP_address = "216.3.122.18"
seg_1_len = IP_address.find('.') # returns -1 if there is no '.'
或
或
使用while的解决方案可以非常简单:
seg_1_length = 1
while IP_address[seg_1_length] != '.':
seg_1_length += 1
假设我们知道输入是有效的ip4地址,它必须至少包含一个“.”,并且不能以“.”开头
如果您不能假定输入是有效的,则需要更具防御性地编写代码,例如:
i = 0
while i < len(ipaddy) and ipaddy[i] != '.':
i += 1
assert i > 0, "ip address can't start with a dot"
assert i <= 3, "the first octet can't be more than 3 characters long"
seg_1_length = i
有几种方法
此方法最容易理解:
IP_address = "216.3.122.18"
i = 0
while i <= len(IP_address) - 1 and IP_address[i] != ".":
i += 1
seg_1_len = i
有几种方法
此方法最容易理解:
IP_address = "216.3.122.18"
i = 0
while i <= len(IP_address) - 1 and IP_address[i] != ".":
i += 1
seg_1_len = i
请给出一个答案——不工作到底意味着什么?一个明显的问题是,您有一个while循环,它不包含任何改变其条件值的内容,因此它要么永远不会开始,要么永远不会结束。为什么要使用while循环?lenIP_地址。拆分“”。[0]将完成此任务。您的问题是否已得到回答?请给出一个-不工作到底意味着什么?一个明显的问题是,您有一个while循环,它不包含任何改变其条件值的内容,因此它要么永远不会开始,要么永远不会结束。为什么要使用while循环?lenIP_地址。拆分“”。[0]将完成此任务。您的问题是否已得到回答?这不是问题python@thebjorn是的!谢谢你的编辑。您还可以使用that's not来摆脱笨拙的-1python@thebjorn是的!谢谢你的编辑。您还可以使用