题解 | #合法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

  1. syntax error
  2. logical error (Exception)

图片说明

图片说明
图片说明


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

全部评论

相关推荐

SinyWu:七院电话面的时候问我有没有女朋友,一听异地说你赶紧分。我:???
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务