인간 과 컴퓨터 의 대화 프로그램 python

6111 단어 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?
--') 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?
--') 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()
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기