编写函数,判断输入的ipv4地址的合法性,ipv4地址形如:128.25.11.220。四部分组成,每部分大于等于0小于等于255。中间用英文句点分隔。
def just(ip):
legal = 0
if type(ip)== str and ip.count('.')==3:
ip1 = ''
l = 0
for j in ip:
l+=1
if j == '.':
if int(ip1) >= 0 and int(ip1) <= 255:
legal+=1
ip1=''
else:
ip1 += j
if l== len(ip) and int(ip1) >= 0 and int(ip1) <= 255:
legal+=1
if legal != 4:#判断合法部分是否有四个,若不是则不合法
print('ipv4地址不合法')
else:
print('ipv4地址合法')
else:
print('ipv4地址不合法')
just('128.25.11.220')
just('555.25.11.220')
结果: