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

标签 python chatbot

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

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

对更改的解释会对我有很大帮助,谢谢。

最佳答案

我不会编辑您的代码,而是给您一个最小的示例,因为这毕竟是我们学习的方式。因此,从我的角度来看,您绝对可以使用 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."

关于python - 每次用户再次询问时如何更改响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59092444/

相关文章:

python - 编写一个引用实例和类的python方法

azure - 如何在azure中部署机器人框架v3机器人?

python - 使用 twisted 和 wokkel 的聊天机器人

artificial-intelligence - 尝试启用 Dialogflow Messenger 时出现错误

chatbot - 使用模式创建的 Watson Assistant 实体未在上下文变量中设置匹配的值

javascript - 使用DirectLine时如何修改Microsoft Chatbot的webchat容器

Python警告控件

python - Pandas 应用于数据框列以返回带有后缀的多列

python - 将标记图像转换为 { label : [coordinates] } 字典的快速方法

python - 使用外部 POST 请求绕过 Django CSRF 保护