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 将RGB图像转换为Pytho灰度图像的实例
问题:我正尝试使用matplotlib读取RGB图像并将其转换为灰度。在matlab中,我使用这个:img=rgb2gray(imread('image.png'));在matplotlibtutorial中他们没有覆盖它。他们
python中os和sys模块的区别与常用方法总结
前言本文主要介绍了关于python中os和sys模块区别与常用方法的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。官方解
Python利用multiprocessing实现最简单的分布式作业调度系统实例
介绍Python的multiprocessing模块不但支持多进程,其中managers子模块还支持把多进程分布到多台机器上。一个服务进程可以作为调度者,将任务分布到其他多
python人机交互_人机交互程序 python实现人机对话相关推荐
- Php扫码签到功能怎么实现,python扫码签到程序python中如何定义类
什么是类? 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 什么是方法? 类中的函数即为方法 如何定义一个类? 定义类,语法格式如下: < ...
- python开发windows桌面程序,python开发windows桌面应用
为什么用Python开发桌面应用程序 1)wxWidgets是一个比MFC优雅的库,TortoiseCVS用wxWidges而不用MFC,就是因为wxWidgets好用,而不是为了可以移植. 2)Py ...
- python 购物车程序_购物车程序python
广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 第3点要求: 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒在循环外层 ...
- python书籍_最好的Python书籍,让您像专业程序员一样编程
Python是一种值得学习的语言.学习Python不仅会激发你用最简单的方式完成高度复杂的任务,还会打破传统编程范型的藩篱.Python是能够改变你的编程方式进而改变你看问题的角度的一种语言. Pyt ...
- 有趣的超短python代码_有趣的python精短程序
python可以简单优美,也很有趣,下面是收集的例子: 1.一句话开始一个http的文件服务器: $ python -m SimpleHTTPServer Serving HTTP on 0.0.0. ...
- 学习python课程_想学习Python吗? 这是我们的免费4小时互动课程
学习python课程 Python is a popular, versatile and easy-to-learn language. It's the go-to language for AI ...
- 2018年python薪资_最好的Python:2017和2018年至今我最喜欢的文章集
2018年python薪资 我打算发布此收藏集 (My intention with publishing this collection) Last year I only used Medium ...
- python股市_如何使用python和破折号创建仪表板来主导股市
python股市 始终关注大局 (Keep Your Eyes on the Big Picture) I've been fascinated with the stock market since ...
- 少儿编程python教材_少儿编程|Python环境安装
一.为什么要学Python? 小学 山东,浙江,北京地区小学已将Python内容纳入教材 高中 浙江省已将信息技术教材编程语言替换为Python 大学 计算机二级考试加入"Python 语言 ...
- python开发微信小程序-Python 开发者的微信小程序开发实践
导读 在知乎上,有人提问"如何使用 Python 开发微信小程序". 其实微信小程序作为一个前端的机制,Python 并不能插上边.只不过可以作为后端接口为微信小程序提供数据服务而 ...
最新文章
- 2万字看完腾讯最纯粹的一届WE大会:从黑洞、虫洞到克隆猴
- 通过OWA修改密码,提示您输入的密码不符合最低安全要求
- 日常生活小技巧 -- word中插入visio和视频
- IE10,IE9透明元素无法点击
- intent android 匿名,Android 匿名启动activity 启动系统activity
- 判别分析分为r型和q型吗_SPSS聚类和判别分析参考.ppt
- lnmp yum安装mysql_centos5 yum安装lnmp
- 【HISI系列】之内存开辟的方法
- jQuery源码分析一
- 多节点什么网络取得技术性突破_中国学者研究“多节点量子网络”取得基础性突破...
- webrtc与stunserver、turnserver建立连接花费时间十秒左右
- 蒙太奇服务器维修,蒙太奇服务器多台互连导片方法.pdf
- 隆重纪念鲁宾逊诞辰,不走样,不离谱
- 2022超级好用的接口自动化测试框架:基于python+requests+pytest+allure实现
- vue 使用高德地图标记坐标,去除高德水印logo
- flutter Gradle build failed to produce an .apk file. xxx\build,but the tool couldn‘t find it.
- 深圳房产之一——直播思考
- EPICS记录参考--Histogram记录(histogram)
- Hadoop那些事儿(四)---MapReduce编程实例(基础)
- M1 Mac无法安装达芬奇17,怎么办?