题解 | #配置文件恢复#

配置文件恢复

https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5

条件判断,将输入字符串的长度作为命令字符串的切片长度,得到的切片再和输入字符串对比,如果输入为一个单词,则只比较一个切片,如果输入为两个单词,则比较两次,用and连接.
command = ['reset', 'reset board', 'board add', 'board delete', 'reboot backplane', 'backplane abort']
result = ['reset what', 'board fault', 'where to add', 'no board at all', 'impossible', 'install first']
while True:
    try:
        s = input().split()
        if len(s) < 1 or len(s) > 2:
            print('unknown command')
        elif len(s) == 1:
            if s[0] == command[0][:len(s[0])]:
                print(result[0])
            else:
                print('unknown command')
        else:
            l = []
            for c in command[1:]:
                i = c.split()
                if s[0] == i[0][:len(s[0])] and s[1] == i[1][:len(s[1])]:
                    index = command.index(c)
                    l.append(result[index])
            if len(l) == 1:
                print(l[0])
            else:
                print('unknown command')
    except:
        break


全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

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