如何为项目创建聊天机器人,如何再次运行功能?

时间:2019-11-26 11:57:44

标签: python project 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")
     elif n=="What's your name?" or n=='what is your name?' or n=='whats your name?' or n=='whats your name':
         print("BOT: My name is ChatBot :)")
     elif n=="":
         print("Ask something")
     else:
         print("BOT: Sorry :( I can't respond to that,Try asking another question.")
print("BOT: HEY IM CHATBOT MAY I KNOW YOUR NAME?")
a=input('YOU:')
print("BOT: Hi",a.upper())
print("BOT: ASK ME SOMETHING :)")
n=input("YOU:").lower()
#PRINT("BOT: ASK ME AGAIN") 
#N=INPUT("YOU:")SHOULD BE DONE AFTER THE ABOVE ONE HAS COMPLETED
chatbot(n)

我希望使用对话型聊天机器人进行响应并轻松检查该响应。我是一个初学者,我希望代码简单。

1 个答案:

答案 0 :(得分:1)

使用while循环

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")
    elif n=="What's your name?" or n=='what is your name?' or n=='whats your name?' or n=='whats your name':
        print("BOT: My name is ChatBot :)")
    elif n=="":
        print("Ask something")
    else:
        print("BOT: Sorry :( I can't respond to that,Try asking another question.")
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 :)")
    n=input("YOU:").lower()
    if n != 'q':
        chatbot(n)
    else:
        print("BOT: BYE :)")
        break

它会一直重复直到您输入q