# I am stuck at number_of _happy_tweets

Can someone give me a a hint on how to solve this problem. I am stuck

1 Like

No problem, Iâ€™ve searched the forum for you:

2 Likes

Thanks a lot.iâ€™ve submitted my first assignment

1 Like

Hi â€¦ i stuck at this point â€¦i write a code but it is not working properly â€¦giving unexpected value

# store the final answer in this variable

number_of_happy_tweets = 0

for happy_words in sample_tweet:
if word in sample_tweet:
number_of_happy_tweets+=1
print(number_of_happy_tweets)

# perform the calculations here

can anyone help here?

What is the question?

The same questions â€¦
Q13: Determine the number of tweets in the dataset that can be classified as happy.

Number_of_happy_tweet=0

For tweet in tweets:
For word in happy_tweet:
If word in tweet:
Number_of_happy_tweet+=1
What I did was to create a for loop for the main tweet.
Same thing is done for the happy_tweet.

create an if conditional statement which states:
If a word from the happy_tweet is found in the main tweet add one to the number_of_happy_tweet which is assigned the value of zero.

still â€¦didnâ€™t abel to get thatâ€¦can you explain more briefly?

Code:

Explanation:

1. We are looping through all the tweets one by oneâ€¦

2. Now for the nth tweet we are looping through all the wordsâ€¦

3. Next we are checking if the mth word is present in the nth tweet or notâ€¦

4. If it is present then we increasing number_of_happy_tweets by 1 and breaking out of the loop so that other words would not get checked otherwise it will give wrong results (but for this particular question you donâ€™t need to use break as the tweets are given in such a way that only one happy or sad word is present in the tweet).

Hope you will understandâ€¦

4 Likes

thank you so much @vinaypratapsingh609 i perfectly understood it.

i also did the sameâ€¦but I m also thinking of using the split function to split each tweet then check with the given word (logical) and sum those logical =. if greater than equal to one; I count as one otherwise 0. i havenâ€™t tried this outâ€¦is the logic good?

I got your program. Though I have a query. why we applied nested for loop?

We have a list of `tweets`, and we want to compare a word in `happy_words` with the words in a single `tweet` among all the `tweets`.

So, the 1st `for` loop (`for tweet in tweets:`) loop through all the tweets one by one.

Now, the nested or 2nd `for` loop (`for word in happy_words:`) loops through all the words in the `happy_words` list.

And then the `if` statement (`if word in tweet`) compares the `word` with every word in `tweet` and updates the counter (`number_of_happy_tweets`) when True.

2 Likes

number_of_happy_tweets = 0
for x in range (0,len(tweets)):
for word in happy_words:
if word in tweets[x]:
number_of_happy_tweets = number_of_happy_tweets + 1

print(â€śNumber of happy tweets:â€ť, number_of_happy_tweets)

Thank you very much. You made this easy

I appreciate you