In assignment 1, problem 5 I am still not figuring out how to do the iteration. I managed to sort the correct result with this code: (…) number_of_happy_tweets = number_of_happy_tweets
perform the calculations here
tweets_not_with_happy_words = 4
for word in tweets:
for word in happy_words:
if word not in tweets:
number_of_happy_tweets = number_of_tweets - tweets_not_with_happy_words
print(number_of_happy_tweets) .
But this is not a correct loop iteration, given that I declare a variable and the calculation is based only on variable declaration. How can I unstuck…?!
I honestly have no idea what you want to achieve here.
Problems:
You have 2 for loops, both with the same variable name.
You’re checking for word not in tweets, which is true, because tweets usually contain more words (single-word tweets perhaps exists, but not in this assignment).
No idea why you’re counting the not happy words, if you can count just the happy ones.
In my opinion your condition should look like this: word in tweet where word variable is one of the words from happy_words list, and tweet variable is one tweet from tweets list.
Step 1: firstly, get each tweet one by one from the tweets list in a loop
Step 2: then, you need to get each happy word from the list - happy words with another loop inside the first loop
Step 3: then you need to check if that selected happy word is available in a particular tweet that we got from the first loop
If it is available, then you will count it under number_of_happy_tweets
# step 1
for tweet in tweets:
# step 2
for word in happy_words:
#step 3
if word in tweet:
number_of_happy_tweets += 1
I finally figure it out. It was by checking the office hours discusssion on youtube.The final result was very similar to the one proposed here. I wasn’t using the former loop where you iterate over a boolean variable. That’s clever!
I finally figure it out. It was by checking the office hours discusssion on youtube.The final result was very similar to the one proposed here. I wasn’t using the former loop where you iterate over a boolean variable. That’s clever!