1 def Islegal(a): 2 ip = list(map(int,a)) 3 flag = 0 #标志是否合法 4 if 0<=ip[0]<=255: 5 if 0<=ip[1]<=255: 6 if 0 <= ip[2] <= 255: 7 if 0 <= ip[3] <= 255: 8 flag = 1 9 if flag==0: 10 print("NO") 11 return 12 if ip[0]<=127: 13 print("A") 14 return 15 if 128<=ip[0]<=191: 16 print("B") 17 return 18 if 192<=ip[0]<=223: 19 print("C") 20 return 21 if 224<=ip[0]<=239: 22 print("D") 23 return 24 if 240 <= ip[0] <= 255: 25 print("E") 26 return 27 28 def func(): 29 while True: 30 #EOFError的作用就是执行到文件末尾然后抛出异常 31 #try...except捕捉到异常然后执行break语句终止循环 32 try: 33 a = input() 34 a = a.split('.',3) 35 Islegal(a) 36 except EOFError: 37 break 38 39 40 if __name__ == '__main__': 41 func()
标签:合法性,检查,网络,IP地址,类别,Problem From: https://www.cnblogs.com/hangsingplus/p/17349974.html