国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

人機交互程序 python實現(xiàn)人機對話

2020-02-16 10:41:51
字體:
來源:轉載
供稿:網友

自己隨便寫了一個人機交互的程序。不存在任何智能,只是可以識別姓名,可以記錄對話內容,并保存等到下一次交互時加載。 (推薦面向對象版本)

# 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 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()             
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 麻栗坡县| 安西县| 准格尔旗| 天津市| 临城县| 宁晋县| 阿图什市| 海晏县| 四川省| 津南区| 临颍县| 句容市| 沁阳市| 轮台县| 青海省| 普宁市| 商城县| 徐水县| 林周县| 苏州市| 澄城县| 开封县| 丰原市| 正蓝旗| 淳化县| 高清| 白玉县| 淳安县| 九江县| 彭州市| 砚山县| 浮梁县| 武穴市| 上蔡县| 安吉县| 扶余县| 梨树县| 香河县| 永兴县| 康保县| 柳州市|