最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

人機(jī)交互程序 python實(shí)現(xiàn)人機(jī)對(duì)話

 更新時(shí)間:2017年11月14日 10:01:03   作者:nbu2004  
這篇文章主要為大家詳細(xì)介紹了人機(jī)交互程序,初步實(shí)現(xiàn)python人機(jī)對(duì)話,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

自己隨便寫(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 淺談一下Python中5種下劃線的含義

    淺談一下Python中5種下劃線的含義

    這篇文章主要介紹了Python中5種下劃線的含義,在我們學(xué)習(xí)Python的過(guò)程中,經(jīng)常會(huì)遇到一些帶下劃線的詞,那么不同的下劃線有什么意義呢,一起來(lái)學(xué)習(xí)一下吧
    2023-03-03
  • pycharm查看變量值的4種方法匯總

    pycharm查看變量值的4種方法匯總

    因?yàn)镻ython是腳本語(yǔ)言,不會(huì)進(jìn)行編譯,所以只有執(zhí)行到那一行,才能知道那個(gè)變量的類(lèi)型,下面這篇文章主要給大家介紹了關(guān)于pycharm查看變量值的4種方法,需要的朋友可以參考下
    2022-04-04
  • Python編程中裝飾器的使用示例解析

    Python編程中裝飾器的使用示例解析

    這篇文章主要介紹了Python編程中裝飾器的使用示例解析,包括裝飾函數(shù)和方法,含參的裝飾器以及裝飾類(lèi)這三個(gè)方面,需要的朋友可以參考下
    2016-06-06
  • python threading模塊的使用指南

    python threading模塊的使用指南

    python的thread模塊是底層的模塊,python的threading模塊是對(duì)thread做了一些包裝的,可以更加方便的被使用。本文詳細(xì)的講述了threading模塊的用法
    2021-06-06
  • 利用Python實(shí)現(xiàn)從PDF到CSV的轉(zhuǎn)換

    利用Python實(shí)現(xiàn)從PDF到CSV的轉(zhuǎn)換

    將PDF轉(zhuǎn)換為CSV極大地提升了數(shù)據(jù)的實(shí)用價(jià)值,Python作為一種強(qiáng)大的編程語(yǔ)言,能夠高效完成這一轉(zhuǎn)換任務(wù),本文將介紹如何利用Python實(shí)現(xiàn)從PDF到CSV的轉(zhuǎn)換,需要的朋友可以參考下
    2024-07-07
  • 詳解Python打包分發(fā)工具setuptools

    詳解Python打包分發(fā)工具setuptools

    這篇文章主要介紹了Python打包分發(fā)工具setuptools的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-08-08
  • 詳解Python中的編碼問(wèn)題(encoding與decode、str與bytes)

    詳解Python中的編碼問(wèn)題(encoding與decode、str與bytes)

    這篇文章主要介紹了Python中的編碼問(wèn)題(encoding與decode、str與bytes),幫助大家更好的理解和使用python進(jìn)行開(kāi)發(fā),感興趣的朋友可以了解下
    2020-09-09
  • Python如何同時(shí)讀寫(xiě)Excel

    Python如何同時(shí)讀寫(xiě)Excel

    這篇文章主要介紹了Python如何同時(shí)讀寫(xiě)Excel問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • python 計(jì)算積分圖和haar特征的實(shí)例代碼

    python 計(jì)算積分圖和haar特征的實(shí)例代碼

    今天小編就為大家分享一篇python 計(jì)算積分圖和haar特征的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-11-11
  • python讀寫(xiě)文件write和flush的實(shí)現(xiàn)方式

    python讀寫(xiě)文件write和flush的實(shí)現(xiàn)方式

    今天小編就為大家分享一篇python讀寫(xiě)文件write和flush的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-02-02

最新評(píng)論

苍山县| 闻喜县| 屯门区| 宝丰县| 文水县| 旬邑县| 托里县| 玉环县| 泗洪县| 南丹县| 阳泉市| 涿州市| 宜兴市| 灯塔市| 青铜峡市| 招远市| 临洮县| 全南县| 广昌县| 土默特左旗| 忻州市| 长春市| 靖西县| 济阳县| 杭州市| 景德镇市| 张家口市| 孟州市| 包头市| 忻州市| 广水市| 宁南县| 九寨沟县| 大悟县| 灵武市| 博白县| 唐山市| 门头沟区| 鄱阳县| 深州市| 阆中市|