1

How to stop infinite loop in simple chatbot

I have a simple chatbot with the following code

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

What I wanted was that if human_input is recognized inside greeting_inputs for the conversation to keep going and if it isn't, then the conversation would stop. But the code above never stops the conversation even if I input nonsense. Why is my else statement never activating?

Submitted May 23rd 2021 by Admin

Answers
0

Update input('Talk to me again: ') to

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

Since next time it asks, you haven't given it a variable to assign the input, it keeps repeating

Admin | 3 months ago



Relevant Questions