建立字典,输入元素遍历字典keys,字典的key为字符串,用字符串count方法判断输入是否匹配

while True:
    try:
        s = input()
        dic = {
            'reset': 'reset what',
            'reset board': 'board fault',
            'board add': 'where to add',
            'board delete': 'no board at all',
            'reboot backplane': 'impossible',
            'backplane abort': 'install first',
            'he he': 'unknown command'
        }
        lst = s.strip().split()  # 输入拆分为列表
        cmd = ''
        if len(lst) == 1:  # 列表长度为1,只匹配字典只包含一个关键字的key
            for i in dic.keys():
                if i.count(' ') < 1: # 判断key是否只包含一个关键字
                    if i.count(lst[0]) == 1:
                        cmd = i
                    else:
                        cmd = 'he he'
        elif len(lst) == 2:  # 列表长度为2,需要在字典包含两个关键字的keys中匹配
            temp = 0
            now_cmd = ''
            for i in dic.keys():
                if i.count(' ') >= 1:
                    if i.split()[0].count(lst[0]) == 1 and i.split()[1].count(lst[1]) == 1:  # 字典key拆分分别匹配输入的两个关键字
                        now_cmd += i
                        temp += 1
            if temp <= 1 and now_cmd:
                cmd = now_cmd
            else:
                cmd = 'he he'
        else:
            cmd = 'he he'

        print(dic.get(cmd, 'unknown command'))
    except:
        break