python能做人机界面吗_人机交互程序 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能做人机界面吗_人机交互程序 python实现人机对话相关推荐
- python人机对话_人机交互程序 python实现人机对话
人机交互程序 python实现人机对话 来源:中文源码网 浏览: 次 日期:2018年9月2日 [下载文档: 人机交互程序 python实现人机对话.txt ] (友情提示:右键点上行txt文档名-& ...
- python能做财务分析吗_您可以使用Python进行财务规划和分析吗?
python能做财务分析吗 问题 (The Problem) If you work in the Financial Planning and Analysis area, chances are ...
- python人机交互_人机交互程序 python实现人机对话
自己随便写了一个人机交互的程序.不存在任何智能,只是可以识别姓名,可以记录对话内容,并保存等到下一次交互时加载. (推荐面向对象版本) # hello.py # 这是老早写的.不过今天加入了Pickl ...
- python人机对话性别年龄名字_人机交互程序 python实现人机对话
自己随便写了一个人机交互的程序.不存在任何智能,只是可以识别姓名,可以记录对话内容,并保存等到下一次交互时加载. (推荐面向对象版本) # hello.py # 这是老早写的.不过今天加入了Pickl ...
- python自己做个定时器_简单实现python定时器
下面介绍以threading模块来实现定时器的方法.(课课家) 使用前先做一个简单试验: import threading def sayhello(): print "hello worl ...
- python主要做什么的_学了Python可以用来干什么呢?
很多家长朋友可能会问,孩子学Python 可以用来干什么呢? 一般学Python可以往很多方面发展: 01 web应用开发 在国内,豆瓣一开始就使用Python作为web开发基础语言,知乎的整个架构也 ...
- python能做什么日常_我用 Python 做些什么?
我主要工作是后端,所以这一点我就不说了.本文主要说下我用 Python 做的其它的一些事. 1 数据导出支撑 一个月可能有一两次需要导出一些数据,每次的需求都有些不同.刚开始的时候还好,用数据库的一些 ...
- python怎么做情感分析_如何用python进行情感分析
我们在计划中遵循以下三个主要步骤:授权twitter API客户端. 向Twitter API发出GET请求以获取特定查询的推文. 解析推文.将每条推文分类为正面,负面或中立. 首先,我们创建一个Tw ...
- Python脚本做一个淘宝秒杀程序!
Python脚本做一个淘宝秒杀程序! 先上代码 from selenium import webdriver import datetime import time import os import ...
最新文章
- springboot过滤器排除掉一些url_理解这9大内置过滤器,才算是精通Shiro
- InetAddress类和InetSocketAddress的使用
- 无重复字符的最长子串【三种解法】--LeetCode
- Java中的同步集合与并发集合有什么区别?
- 通话录音_iOS14,通话录音终于来了!
- android 网络通信框架volly
- 关于秒杀系统优化方向
- 快速的mysql导入导出数据(load data和outfile)
- html+jQuery实现拖动滑块图片拼图验证码插件,移动端适用
- STM8L开发环境配置
- 复制虚拟机出现”适配器 的mac地址在保留地址范围内‘’
- 使用反相器的rc振荡电路
- 信创办公--基于WPS的Word最佳实践系列(利用项目符号及编号条理化文本)
- TypeScript-基础类型学习
- 一文搞懂CAN FD总线协议帧格式
- 错题集:HDLBits lfsr5 Galois型lfsr
- 论文笔记:Tip-Adapter: Training-free CLIP-Adapter for Better Vision-Language Modeling
- ZTE/中兴Blade N880 root教程_方法
- 串口流控(CTS/RTS)使用详解
- [易飞]关于易飞ERP进货单审核过程的总结