一个简易会学习的问答机器人

dict = {
       'hello'            :'hello',
       'nice to meet you' : 'my  pleasure',
       'favorite fruit?'  : 'peach',
       'how old'          :'20 years old',
       'you are cute'     :'thanks'
       }
flag = 'c'
work = True


print 'hi i am p'
print 'do you want to chat'
while flag == 'c' or 't':
  flag =raw_input('talk(c)or train me(t),or leave(l)')
  if flag == 't':
    question = raw_input('input question (key):')
    answer = raw_input('input answer(value):')
    dict[str(question)] = str(answer)
    print 'amazing'
    print 'i have learned %d questions!'%len(dict)
    continue
  elif flag == 'c':
    if len(dict) == 0;
      print 'i feel blank,train me'
      continue;
      
    chat_word = raw_input('what do you want to say')    
    
    for key in sorted(dict.key()):
      if str(chat_word) == key:
        work = True
        print dict[key]
        break
      else:
        work = False
        
    if work == False:
      print 'sorry,no answer'
      work = True
      
  elif flag == 'l':
    print 'ok byebye'
    break
  
  else:
    print 'please input'
    continue

猜你喜欢

转载自blog.csdn.net/qq_21740193/article/details/80200354