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','N'}:
print('--%s. [You say no.]'%y)
elif y in {'yes','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,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','Q'}:
print('[You quit]')
break
if not search(name,self.data):
print('--Welcome,'+name+ '. I will remember you name.')
d={'name':name,'history':[]}
self.data.append(d)
else:
print('--Hi,'+name+'. How I miss you.')
k,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:
import pickle
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','N'}:
print('--%s.'%y)
print('--Bye bye.')
break
elif y in {'yes','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','N'}:
print('--%s. [You say no.]'%y)
elif y in {'yes','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()
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持编程⼩技巧。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。