一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - 人機交互程序 python實現(xiàn)人機對話

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

2020-12-17 00:59nbu2004 Python

這篇文章主要為大家詳細介紹了人機交互程序,初步實現(xiàn)python人機對話,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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的面向對象編程版本(推薦)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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()

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/nbu2004/article/details/49054299

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 四虎精品成人a在线观看 | 丝袜足控免费网站xx动漫漫画 | 日本性生活免费看 | 日韩v| 99热色 | 91se在线 | 星空无限传媒xk8129 | 国产福利视频一区二区微拍视频 | 99久久国产综合精麻豆 | 忘忧草秋观看未满十八 | 校园刺激全黄H全肉细节文 校草让我脱了内裤给全班看 | 天天爱天天做天天爽天天躁 | 91精品国产91热久久久久福利 | 精品国产成人高清在线 | 免费观看日本视频 | 成人亚洲欧美综合 | 成人免费在线视频 | 精品在线99 | 国产一久久香蕉国产线看观看 | 毛片应用 | 毛片在线免费观看网站 | 亚洲骚图 | 日本人泡妞18xxⅹ | 羞羞影院午夜男女爽爽影院网站 | 精品国产乱码久久久久久人妻 | 欧美特黄特色aaa大片免费看 | 天堂69亚洲精品中文字幕 | 99精品热| 国产自精品 | 欧美老人与小伙子性生交 | 97久久久亚洲综合久久88 | 激情婷婷成人亚洲综合 | 精品91一区二区三区 | 亚洲va天堂va国产va久久 | 性印度freehd | 91九色最新地址 | 国产午夜精品久久理论片 | 精品国语国产在线对白 | 亚洲美女人黄网成人女 | 免费看视频高清在线观看 | www视频免费观看 |