该代码仅适用于具有两个段的ip,最后没有句点。 (EX / 123.4) 123.4.won不工作; 123.4.5不会工作(即使最后有一段时间) 我对这里出了什么问题感到困惑。
所以这是我的代码:
ip = str(input("Enter IP address:"))
if ip[-1] != ".":
ip += "."
segment = 1
segment_length = 0
for character in ip:
if character == ".":
print "Segment {} contains {} characters".format(segment, segment_length)
segment += 1
segment_length = 0
else:
segment_length += 1
Traceback (most recent call last):
File "/Users/jack1/Desktop/python_practice.py", line 1, in <module>
ip = str(input("Enter IP address:"))
File "<string>", line 1
123.44.5
^
SyntaxError: unexpected EOF while parsing