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

 更新时间:2017年11月14日 10:01:03   作者:nbu2004  
这篇文章主要为大家详细介绍了人机交互程序,初步实现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 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() 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 浅谈一下Python中5种下划线的含义

    浅谈一下Python中5种下划线的含义

    这篇文章主要介绍了Python中5种下划线的含义,在我们学习Python的过程中,经常会遇到一些带下划线的词,那么不同的下划线有什么意义呢,一起来学习一下吧
    2023-03-03
  • pycharm查看变量值的4种方法汇总

    pycharm查看变量值的4种方法汇总

    因为Python是脚本语言,不会进行编译,所以只有执行到那一行,才能知道那个变量的类型,下面这篇文章主要给大家介绍了关于pycharm查看变量值的4种方法,需要的朋友可以参考下
    2022-04-04
  • Python编程中装饰器的使用示例解析

    Python编程中装饰器的使用示例解析

    这篇文章主要介绍了Python编程中装饰器的使用示例解析,包括装饰函数和方法,含参的装饰器以及装饰类这三个方面,需要的朋友可以参考下
    2016-06-06
  • python threading模块的使用指南

    python threading模块的使用指南

    python的thread模块是底层的模块,python的threading模块是对thread做了一些包装的,可以更加方便的被使用。本文详细的讲述了threading模块的用法
    2021-06-06
  • 利用Python实现从PDF到CSV的转换

    利用Python实现从PDF到CSV的转换

    将PDF转换为CSV极大地提升了数据的实用价值,Python作为一种强大的编程语言,能够高效完成这一转换任务,本文将介绍如何利用Python实现从PDF到CSV的转换,需要的朋友可以参考下
    2024-07-07
  • 详解Python打包分发工具setuptools

    详解Python打包分发工具setuptools

    这篇文章主要介绍了Python打包分发工具setuptools的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-08-08
  • 详解Python中的编码问题(encoding与decode、str与bytes)

    详解Python中的编码问题(encoding与decode、str与bytes)

    这篇文章主要介绍了Python中的编码问题(encoding与decode、str与bytes),帮助大家更好的理解和使用python进行开发,感兴趣的朋友可以了解下
    2020-09-09
  • Python如何同时读写Excel

    Python如何同时读写Excel

    这篇文章主要介绍了Python如何同时读写Excel问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • python 计算积分图和haar特征的实例代码

    python 计算积分图和haar特征的实例代码

    今天小编就为大家分享一篇python 计算积分图和haar特征的实例代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-11-11
  • python读写文件write和flush的实现方式

    python读写文件write和flush的实现方式

    今天小编就为大家分享一篇python读写文件write和flush的实现方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02

最新评论