人機(jī)交互程序 python實(shí)現(xiàn)人機(jī)對(duì)話
自己隨便寫(xiě)了一個(gè)人機(jī)交互的程序。不存在任何智能,只是可以識(shí)別姓名,可以記錄對(duì)話內(nèi)容,并保存等到下一次交互時(shí)加載。 (推薦面向?qū)ο蟀姹荆?/span>
# hello.py # 這是老早寫(xiě)的。不過(guò)今天加入了Pickle,然后潤(rùn)色了一下。 # 可能有點(diǎn)無(wú)聊(不推薦使用) 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的面向?qū)ο缶幊贪姹荆ㄍ扑])
# 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 saved 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()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 在Python程序員面試中被問(wèn)的最多的10道題
- Python使用django框架實(shí)現(xiàn)多人在線匿名聊天的小程序
- Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的驗(yàn)證碼程序
- Python基于time模塊求程序運(yùn)行時(shí)間的方法
- python 隨機(jī)數(shù)使用方法,推導(dǎo)以及字符串,雙色球小程序?qū)嵗?/a>
- 在Python的一段程序中如何使用多次事件循環(huán)詳解
- Python初學(xué)時(shí)購(gòu)物車程序練習(xí)實(shí)例(推薦)
- Python實(shí)現(xiàn)比較撲克牌大小程序代碼示例
相關(guān)文章
從Pytorch模型pth文件中讀取參數(shù)成numpy矩陣的操作
這篇文章主要介紹了從Pytorch模型pth文件中讀取參數(shù)成numpy矩陣的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03Python中使用tkFileDialog實(shí)現(xiàn)文件選擇、保存和路徑選擇
這篇文章主要介紹了Python中使用tkFileDialog實(shí)現(xiàn)文件選擇、保存和路徑選擇,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05插入排序_Python與PHP的實(shí)現(xiàn)版(推薦)
下面小編就為大家?guī)?lái)一篇插入排序_Python與PHP的實(shí)現(xiàn)版(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05Python基于分析Ajax請(qǐng)求實(shí)現(xiàn)抓取今日頭條街拍圖集功能示例
這篇文章主要介紹了Python基于分析Ajax請(qǐng)求實(shí)現(xiàn)抓取今日頭條街拍圖集功能,涉及Python針對(duì)今日頭條URL請(qǐng)求與json數(shù)據(jù)處理相關(guān)操作技巧,需要的朋友可以參考下2018-07-07使用Python的Treq on Twisted來(lái)進(jìn)行HTTP壓力測(cè)試
這篇文章主要介紹了使用Python的Treq on Twisted來(lái)進(jìn)行HTTP壓力測(cè)試,基于Python中的Twisted框架,需要的朋友可以參考下2015-04-04淺談python 調(diào)用open()打開(kāi)文件時(shí)路徑出錯(cuò)的原因
這篇文章主要介紹了淺談python 調(diào)用open()打開(kāi)文件時(shí)路徑出錯(cuò)的原因,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06