def Greeting(sentence) :
Greeting_Keywords = ["Hi", "Hi", "Hi", "Hi"]
Greeting_Responses = ["Hi", "Hi", "Hi", "Hello"]
for word in sentence:
if word.lower() in Greeting_Keywords :
return random.choice(Greeting_Responses)
question = input ('Enter:')
print(Greeting(question))
I'm going to make a chatbot with Python When enabled
Enter: Bye
None
It comes out like this.
If I enter a sentence that contains a specific phrase, I want to get one of the answers I set up, what should I do?
If you do for word in sentiment
, sentence
is a string, so for each character of the string you entered is repeated. It's no wonder the result is None
.
What you want to do is to repeat Greeting_Keywords
and check if sentence.lower()
contains the greeting keyword. I made a demo so please run it and check it out.
© 2024 OneMinuteCode. All rights reserved.