I'm new to coding and I would like the user to answer to the following question : " How is your mood today? " and based on the answer the program to print a message.
mood=input('How is your mood today? ')
is_mood_good = True
is_mood_bad = False
if is_mood_good:
print("That's good!")
print("Do something lovely and enjoy your day!")
elif is_mood_bad:
print("I'm sorry to hear that")
print("Do something lovely to cheer yourself up.")
else:
print("Do something you like and enjoy your day")
How do I make the program recognize a certain word and associate it with either a good mood or a bad one then print the message? What do I have to learn in order to accomplish this?
Thank you!
CodePudding user response:
The code you entered
is_mood_good = True
is_mood_bad = False
doesn't have a meaning, the code won't know if the input is good or bad.
Full code:
mood=input('How is your mood today? ')
is_mood_good = ['fine', 'good', 'happy']
is_mood_bad = ['bad', 'sad', 'crying']
mood = mood.lower()
if mood in is_mood_good:
print("That's good!")
print("Do something lovely and enjoy your day!")
elif mood in is_mood_bad:
print("I'm sorry to hear that")
print("Do something lovely to cheer yourself up.")
else:
print("Do something you like and enjoy your day")
Here I created a list of words which the code will recognize and based on it, the code will print the answer.
You can add more words as you like.
CodePudding user response:
Best way is of course to use some machine-learning because it is often very difficult to find an emotional pattern. You could read about NLP and text classification, but I would first recommend some simpler solutions as you are new to it. Check TheDiamondCreeper answer
CodePudding user response:
The above answer by TheDiamondCreeper is perfect for beginners. However you can refer this solution after you become familiar with nltk and nlp
#pip3 install nltk ( run this command on your terminal without the "#")
import nltk
nltk.download('vader_lexicon')
from nltk.sentiment import SentimentIntensityAnalyzer
sia = SentimentIntensityAnalyzer()
mood = input("How is your mood today?")
mood_score= sia.polarity_scores(mood)
if (max(mood_score) =='pos'):
print("That's good!")
print("Do something lovely and enjoy your day!")
elif (max(mood_score) == 'neg'):
print("I'm sorry to hear that")
print("Do something lovely to cheer yourself up.")
else:
print("Do something you like and enjoy your day")
CodePudding user response:
Man, Your Code makes no sense. This is the correct code.
mood=input('How is your mood today? ')
if "good" in mood or "happy" in mood:
print("That's good!")
print("Do something lovely and enjoy your day!")
elif "sad" in mood or "lonely" in mood:
print("I'm sorry to hear that")
print("Do something lovely to cheer yourself up.")
else:
print("Do something you like and enjoy your day")
Now in means that
"sad" in mood
if there is "sad" in mood then it returns true else it returns false.
Its easy.
Keep practicing and learn more