人机交互程序 python实现人机对话
自己随便写了一个人机交互的程序。不存在任何智能,只是可以识别姓名,可以记录对话内容,并保存等到下一次交互时加载。(推荐面向对象版本)
#hello.py #这是老早写的。不过今天加入了Pickle,然后润色了一下。 #可能有点无聊(不推荐使用) importpickle importos.path defsearch(x,data): fork,dinenumerate(data): ifx==d['name']: returnk,d defsave_data(A,pklname): withopen(pklname,'wb')aspkl: pickle.dump(A,pkl) defload_data(pklname): withopen(pklname,'rb')aspkl: returnpickle.load(pkl) #communicatingwithcomputer data=[]ifnotos.path.isfile('data')elseload_data('data') whileTrue: print('Welcome![type"quit"ifyouwanttoquit.]') name=input('--Whatisyourname?\n--') ifnamein{'quit','Quit','q','Q'}: print('[Youquit]') break ifnotsearch(name,data): print('--Welcome,'+name+'.Iwillrememberyouname.') d={'name':name,'age':0,'history':[]} data.append(d) else: print('--Hi,'+name+'.HowImissyou.') k,d=search(name,data) whiled['age']==0: age=input('--Howoldareyou?[Iwillrepeatuntilyourespond!]') try: ifint(age)==0:continue d['age']=int(age);data[k]=d except: pass whileTrue: y=input('--ChanIhelpyou?[yes/no]') whilenoty: y=input('--Yesorno?') d['history'].append(y);data[k]=d ifyin{'no','No','n','N'}: print('--%s.'%y) print('--Byebye.') break elifyin{'yes','Yes','y','Y'}: print('--%s.'%y) print('Iampleasedtoserveyou.') else: print('Iamsorry.Icannotunderstandwhatyousaid.') break #savedata y=input('--Doyouwanttosavethedata?[yes/no]') whilenoty: y=input('--Yesorno?') ifyin{'no','No','n','N'}: print('--%s.[Yousayno.]'%y) elifyin{'yes','Yes','y','Y'}: print('--%s.[thedataissavedinfilenamed"data".]'%y) save_data(data,'data') else: print('Iamsorry.Icannotunderstandwhatyousaid.dataarenotsaved.')
下面是hello.py的面向对象编程版本(推荐)
#hello.py importpickle importos.path defsearch(x,data): fork,dinenumerate(data): ifx==d['name']: returnk,d classHello: def__init__(self,name='',data=None): self.name=name self.data=data def__getstate__(self): returnself.data def__setstate__(self,data): self.data=data defmainloop(self): whileTrue: print('Welcome![type"quit"ifyouwanttoquit.]') name=input('--Whatisyourname?\n--') ifnamein{'quit','Quit','q','Q'}: print('[Youquit]') break ifnotsearch(name,self.data): print('--Welcome,'+name+'.Iwillrememberyouname.') d={'name':name,'age':0,'history':[]} self.data.append(d) else: print('--Hi,'+name+'.HowImissyou.') k,d=search(name,self.data) whiled['age']==0: age=input('--Howoldareyou?[Iwillrepeatuntilyourespond!]') try: ifint(age)==0:continue d['age']=int(age);self.data[k]=d except: pass whileTrue: y=input('--ChanIhelpyou?[yes/no]') whilenoty: y=input('--Yesorno?') d['history'].append(y);self.data[k]=d ifyin{'no','No','n','N'}: print('--%s.'%y) print('--Byebye.') break elifyin{'yes','Yes','y','Y'}: print('--%s.'%y) print('Iampleasedtoserveyou.') else: print('Iamsorry.Icannotunderstandwhatyousaid.') break #savedata y=input('--Doyouwanttosavethedata?[yes/no]') whilenoty: y=input('--Yesorno?') ifyin{'no','No','n','N'}: print('--%s.[Yousayno.]'%y) elifyin{'yes','Yes','y','Y'}: print('--%s.[thedataissavedinfilenamed"data.pkl".]'%y) withopen('data.pkl','wb')asfo: pickle.dump(self,fo) else: print('Iamsorry.Icannotunderstandwhatyousaid.dataarenotsaved.') #communicatingwithcomputer try: withopen('data.pkl','rb')asfo: hello=pickle.load(fo) except: hello=Hello('ai',[]) hello.mainloop()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。