每次用户再次询问时如何更改响应?

时间:2019-11-28 15:50:46

标签: python chatbot

我希望机器人对同一问题做出不同的反应。请帮助我,我知道我很傻,但是我正在学习编码。

def chatbot(n):
 if n=="ARE U REAL?" or n=="are u real?" or n=="are you real" or n=="are you real" or n=="Are you real?" or n=="are u real":
    print("BOT: YES, As Real as You.") #or print("BOT:Yes") #or print("BOT:OFCOURSE")#if asked again should change its reply

#WHATS YOUR NAME

 elif n=="What's your name?" or n=='what is your name?' or n=='whats your name?' or n=='whats your name' or n=='what is your name':  
    print("BOT: My software says that My name is ChatBot \U0001F604 ")
 else:
    print("BOT: Sorry I can't respond to that,Try asking another question")
    prompt=input('Or Do you want to search again on Google? Y=Yes and N=No:').lower()
    if prompt=='y' or prompt=='yes':
        query = input("Search on Google:")
        webbrowser.open("https://google.com/search?q=%s" % query)
    else:
        print("BOT: Ok :)")


    print("BOT: HEY IM CHATBOT MAY I KNOW YOUR NAME?")
    a=input('YOU:')
    print("BOT: Hi",a.upper())

    while True:
        print("BOT: ASK ME SOMETHING",a.upper(), "\U0001F642 OR PRESS q TO EXIT!")
        n=input("YOU:").lower()
        if n != 'q':
           chatbot(n)
    else:
       print("BOT: BYE :)")
       break

解释这些变化会非常感谢我。

2 个答案:

答案 0 :(得分:0)

我不会编辑您的代码,而是给您一个最小的示例,因为这毕竟是我们的学习方法。因此,从我的角度来看,您肯定可以使用random内置库和类似的东西,而不是以一个不变的顺序循环一组预定义的答案:

# import library to choose a random item in a list
from random import choice

# set the list of possible answers
answers = ["Yes, I'm real!", "I am real indeed.", "Would you believe me if I said yes?"]

# choose a random answer from the list of possible answers
answer = choice(answers)

print(answer)

# > "I am real indeed."

答案 1 :(得分:0)

感谢西弗勒斯

from random import choice
 def chatbot(n):
 if n=="ARE U REAL?" or n=="are u real?" or n=="are you real" or n=="are you real" or 
 n=="Are you real?" or n=="are u real":
     answers = ["Yes, I'm real!", "I am real indeed.", "Would you believe me if I 
 said yes?"]
     answer = choice(answers)
     print(answer)

 elif n=="What's your name?" or n=='what is your name?' or n=='whats your name?' or n=='whats your name' or n=='what is your name':  
       answers = ["chatbot!", "I am real chabot.", "Would you believe me if I said siri?"]
       answer = choice(answers)
       print(answer)
 else:
    print("BOT: Sorry I can't respond to that,Try asking another question")
    prompt=input('Or Do you want to search again on Google? Y=Yes and N=No:').lower()
    if prompt=='y' or prompt=='yes':
        query = input("Search on Google:")
        webbrowser.open("https://google.com/search?q=%s" % query)
    else:
        print("BOT: Ok :)")


   print("BOT: HEY IM CHATBOT MAY I KNOW YOUR NAME?")
   a=input('YOU:')
   print("BOT: Hi",a.upper())

   while True:
   print("BOT: ASK ME SOMETHING",a.upper(), "\U0001F642 OR PRESS q TO EXIT!")
   n=input("YOU:").lower()
   if n != 'q':
      chatbot(n)
   else:
      print("BOT: BYE :)")
      break

这正是我所需要的。