dict = { 'Hello' :'Hello', 'Nice to meet you' :'Nice to meet you too', 'Which fruit do you like best' :'I like yaoxu very much', 'How old are you' :'20 year old', 'You are handsome' :'Thank you', } flag = 'c' work = True print ('Hi,my name is Yaoxu.') print ('Do you want chat with me?') while flag == 'c' or 't': flag = input("你可以选择和我聊天(c)还是训练我对话(t),或者让我离开(l)?(c/t/l) ") if flag == "t": question = input("请输入问题(key): ") answer = input("请输入回答(value): ") dict[str(question)] = str(answer) print ("训练成功") print ("现在我已经会%d个问题了!" % len(dict)) continue elif flag == 'c': if len(dict) == 0: print ("现在我还不会任何问题,请先训练我!") continue; chat_word = input("谢谢你跟我聊天,你想对我说点什么? : ") for key in sorted(dict.keys()): if str(chat_word) == key: work = True print ("",dict[key]) break else: work = False if work == False: print ("抱歉,这句话我还不会回答") work = True elif flag == 'l': print ("好的,下次再见") break else: print ("请输入提示的指令") continue
机器人
猜你喜欢
转载自blog.csdn.net/acmer6s/article/details/80315303
今日推荐
周排行