python笨办法,但覆盖全面:
思路:mystr.find(str) # 返回str在mystr中的索引值,-1表示str不在mystr中
#1定义执行函数 def execute(cmd): if len(cmd.split())==1:#若只有一个关键字 if 'reset'.find(cmd)==0 : return 'reset what' else: return 'unknown command' elif len(cmd.split())==2:#若有两个关键字 CMD=cmd.split() if 'reset'.find(CMD[0])==0 and 'board'.find(CMD[1])==0 and 'backplane'.find(CMD[1])==-1: return 'board fault' elif 'board'.find(CMD[0])==0 and 'add'.find(CMD[1])==0 and 'abort'.find(CMD[1])==-1: return 'where to add' elif 'board'.find(CMD[0])==0 and 'delete'.find(CMD[1])==0: return 'no board at all' elif 'reboot'.find(CMD[0])==0 and 'backplane'.find(CMD[1])==0 and 'board'.find(CMD[1])==-1: return 'impossible' elif 'backplane'.find(CMD[0])==0 and 'abort'.find(CMD[1])==0 and 'add'.find(CMD[1])==-1: return 'install first' else: return 'unknown command' else:#若超过两个关键字 return 'unknown command' #2调用已定义的执行函数 while True: try: cmd=input() print(execute(cmd)) except: break