Question 5 : number of happy tweets

Number of happy tweets is still coming out to be 0 even though ive added the words to an empty list?
here is my code please help me with what ive done wrong.

thanks

tweets = [
“Wow, what a great day today!! #sunshine”,
“I feel sad about the things going on around us. #covid19”,
“I’m really excited to learn Python with @JovianML #zerotopandas”,
“This is a really nice song. #linkinpark”,
“The python programming language is useful for data science”,
“Why do bad things happen to me?”,
“Apple announces the release of the new iPhone 12. Fans are excited.”,
“Spent my day with family!! #happy”,
“Check out my blog post on common string operations in Python. #zerotopandas”,
“Freecodecamp has great coding tutorials. #skillup
]

happy_words = [‘great’, ‘excited’, ‘happy’, ‘nice’, ‘wonderful’, ‘amazing’, ‘good’, ‘best’]
number_happy_tweets = 0

for word in happy_words:
if word in tweets:
number_happy_tweets +=1

print(number_happy_tweets)

You’re make a small mistake here. tweets are also a list so you need to run two loops.
First loop : For every tweet in tweets
Second loop ; To check every word in happy words
Then you can count, if the word is in tweet (single tweet)

number_of_happy_tweets = 0

perform the calculations here

for tweet in tweets:
for word in happy_words:
if word in tweets:
number_of_happy_tweets +=1

print(number_of_happy_tweets)

but unable to fetch result

1 Like