1

如何在简单的聊天机器人中停止无限循环

我有一个带有以下代码的简单聊天机器人

import random human_input = input('Talk to me: ') continue_dialogue = True greeting_inputs = ("hey", "good morning", "good evening", "morning", "evening", "hi", "whatsupp") greeting_responses = ["hey", "hey hows you?", "*nods*", "hello, how you doing", "hello", "Welcome, I am good and you"] def generate_greeting_response(input): for token in input.split(): if token.lower() in greeting_inputs: return random.choice(greeting_responses) while continue_dialogue: for token in human_input.split(): if generate_greeting_response(human_input) is not None: print("Chatterbot: " + generate_greeting_response(human_input)) input('Talk to me again: ') else: print("Chatterbot: Bye") continue_dialogue = False

我想要的是,如果greeting_inputs识别出human_input以便对话继续进行,否则,对话将停止。但是,即使我输入废话,上面的代码也永远不会停止对话。为什么我的else语句永远不会激活?

已提交 May 23rd 2021 通過 Admin

答案
0

input('Talk to me again: ')

human_input=input('Talk to me again: ')

自从下一次询问以来,您没有为它分配变量来分配输入,它一直在重复

Admin | 4个月前



枪支相关