题解 | #合法IP#
合法IP
http://www.nowcoder.com/practice/995b8a548827494699dc38c3e2a54ee9
遇到Handling EOFError Exception,解决:
using try and except keywords in Python.
while 1: try: parts = input().split('.') except: break y = 1 for part in parts: if int(part) > 2**8-1 or int(part) <0: print('NO') y = 0 break if y == 1: print('YES')
Method 2:
import ipaddress while True: try: assert(ipaddress.ip_address(input())) print('YES') except Exception as e: if isinstance(e, EOFError):break else:print('NO')
In python, there are 2 kinds of Error
- syntax error
- logical error (Exception)
assert(ipaddress.ip_address('adfasdf'))
会产生ValueError