'Python IF statement asks for input twice before proceeding
I am trying to code a chatbot.
I am working on a function where the user asks the chatbot how it is doing, it breaks my sentence into words and IF a keyword exists in the list, it proceeds to ask the user how they are doing.
I then use an if/elif statement, IF the user states a positive feeling, and that feeling exists in the list of positive feelings, it then should state that it is happy to hear that.
ELSE IF the user states a negative feeling, and that feeling exists in the list of negative feelings, it offers to cheer the user up with a joke.
The problem is this: -The user asks the bot how it is doing
-The bot replies "I'm well, what about you"
-The user replies happy or sad (both are words in according happy-feeling-list and negative-feeling-list)
-The bot replies accordingly (If the user is well, it is happy, if the user isn't, it offers to say a joke)
-No matter what the user inputs afterwards, the bot loops back at the beginning and replies again "I'm well, what about you"
-ONLY If the user repeats the same steps AGAIN, then the process finally continues, with the bot either saying a joke or being happy that the user is well.
Why does this happen? I can't really figure a reason
Here is the function code:
def howareyou(sentence):
for word in sentence.split():
#IF WORD IS IN CHECK UP INPUT LIST
if (word.lower() in HOWAREYOU_INPUTS):
print ("BOT: " + random.choice(HOWAREYOU_RESPONSES) + " What about you?")
#USER STATES IF THEY ARE WELL OR NOT
secondquestion = input("YOU: ")
for word in secondquestion.split():
#IF USER REPLIES POSITIVELY, BOT REPLIES HAPPILY
if (word.lower() in POSITIVE_INPUTS):
return random.choice(ALSOHAPPY_RESPONSES)
#IF USER REPLIES NEGATIVELY, BOT OFFERS TO SAY A JOKE
elif (word.lower() in NEGATIVE_INPUTS):
print ("BOT: Maybe a joke will help?")
jokeoffer = input("YOU: ")
jokeoffer = jokeoffer.lower()
for word in jokeoffer.split():
#USER AGREES TO A JOKE
if (word.lower() in AGREE_INPUTS):
return random.choice(JOKE_LIST)
#USER DECLINES OFFER
else:
return random.choice(GETWELL_RESPONSES)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
