遇到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

  1. syntax error
  2. logical error (Exception)

图片说明

图片说明
图片说明


assert(ipaddress.ip_address('adfasdf'))
会产生ValueError