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

全部评论

相关推荐

2025-12-16 17:17
门头沟学院 产品经理
烤点老白薯:他第二句话的潜台词是想让你帮他点个瑞幸或者喜茶啥的
mt对你说过最有启发的一...
点赞 评论 收藏
分享
菜菜狗🐶:双非之光
找工作,你会甘心进小厂还...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务