• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

人机交互程序 python实现人机对话

python 搞代码 4年前 (2022-01-07) 33次浏览 已收录 0个评论

这篇文章主要为大家详细介绍了人机交互程序,初步实现python人机对话,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

自己随便写了一个人机交互的程序。不存在任何智能,只是可以识别姓名,可以记录对话内容,并保存等到下一次交互时加载。 (推荐面向对象版本)

 # hello.py # 这是老早写的。不过今天加入了Pickle,然后润色了一下。 # 可能有点无聊(不推荐使用) import pickle import os.path def search(x, data): for k, d in enumerate(data): if x == d['name']: return k, d def save_data(A,pklname): with open(pklname, 'wb') as pkl: pickle.dump(A, pkl) def load_data(pklname): with open(pklname, 'rb') as pkl: return pickle.load(pkl) # communicating with computer data=[] if not os.path.isfile('data') else load_data('data') while True: print('Welcome! [type "quit" if you want to quit.]') name=input('--What is your name?\n--') if name in {'quit','Quit','q','Q'}: print('[You quit]') break if not search(name, data): print('--Welcome, '+name+ '. I will remember you name.') d={'name':name,'age':0,'history':[]} data.append(d) else: print('--Hi, '+name+'. How I miss you.') k, d=search(name, data) while d['age']==0: age=input('--How old are you?[I will repeat until you respond!]') try: if int(age)==0: continue d['age']=int(age); data[k]=d except: pass while True: y=input('--Chan I help you? [yes/no]') while not y: y=input('--Yes or no?') d['history'].append(y); data[k]=d if y in {'no','No','n','N'}: print('--%s.'%y) print('--Bye bye.') break elif y in {'yes','Yes','y','Y'}: print('--%s.'%y) print('I am pleased to serve you.') else: print('I am sorry. I can not understand what you said.') break # save data y=input('--Do you want to save the data? [yes/no]') while not y: y=input('--Yes or no?') if y in {'no','No','n','N'}: print('--%s. [You say no.]'%y) elif y in {'yes','Yes','y','Y'}: print('--%s. [the data is saved in file named "data".]'%y) save_data(data, 'data') else: print('I am sorry. I can not understand what you said. data are not saved.') 

下面是hello.py的面向对象编程版本(推荐)

 # hello.py import pickle import os.path def search(x, data): for k, d in enumerate(data): if x == d['name']: return k, d class Hello: def __init__(self, name='', data=None): self.name = name self.data = data def __getstate__(self): return self.data def __setstate__(self, data): self.data = data def mainloop(self): while True: print('Welcome! [type "quit" if you want to quit.]') name=input('--What is your name?\n--') if name in {'quit','Quit','q','Q'}: print('[You quit]') break if not search(name, self.data): print('--Welcome, '+name+ '. I will remember you name.') d={'name':name,'age':0,'history':[]} self.data.append(d) else: print('--Hi, '+name+'. How I miss you.') k, d=search(name, self.data) while d['age']==0: age=input('--How old are you?[I will repeat until you respond!]') try: if int(age)==0: continue d['age']=int(age); self.data[k]=d except: pass while True: y=input('--Chan I help you? [yes/no]') while not y: y=input('--Yes or no?') d['history'].append(y); self.data[k]=d if y in {'no','No','n','N'}: print('--%s.'%y) print('--Bye bye.') break elif y in {'yes','Yes','y','Y'}: print('--%s.'%y) print('I am pleased to serve you.') else: print('I am sorry. I can not understand what you said.') break # save data y=input('--Do you want to save the data? [yes/no]') while not y: y=input('--Yes or no?') if y in {'no','No','n','N'}: print('--%s. [You say no.]'%y) elif y in {'yes','Yes','y','Y'}: print('--%s. [the data is sa<span style="color:transparent">来源gaodai#ma#com搞*!代#%^码网</span>ved in file named "data.pkl".]'%y) with open('data.pkl', 'wb') as fo: pickle.dump(self, fo) else: print('I am sorry. I can not understand what you said. data are not saved.') # communicating with computer try: with open('data.pkl', 'rb') as fo: hello=pickle.load(fo) except: hello=Hello('ai',[]) hello.mainloop() 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持gaodaima搞代码网

以上就是人机交互程序 python实现人机对话的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:人机交互程序 python实现人机对话
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址