2

I am trying to search from a list in python. Whenever I input the search term it always say "No tweets contained search", even when I know that the term is in the list. What am I doing wrong?

elif(ch==3):
        match = 0
        tweetlist.reverse()
        if tweetlist == []:
            print("There are no tweets to search.\n")
            continue
        else:
            search = input("What would you like to search for? ")
            for tweets in tweetlist:
                if(search in tweetlist):
                    match = 1
                if match == 1:
                    print ("Search Results")
                    print("----------")
                    print(tweets.get_author(), "-", tweets.get_age())
                    print(tweets.get_text(), "\n")
                elif match == 0:
                    print("No tweets contained,", search, "\n")
2
  • Could be a bad copy/paste but "if match == 1:" should is indented too far Commented Dec 9, 2015 at 22:10
  • Do you want to search for a text that contains the specified 'search' criteria? Or you want to search in general for all the occurrences (author,age etc) ? if search in tweets.get_text(): match = 1 Commented Dec 9, 2015 at 22:23

1 Answer 1

1

Try this:

 ...
 matched_tweets = []
 if search in tweets.get_text(): 
     match = 1
     matched_tweets.append(tweets) # append all the matches to an array
 ...

 # to print out the results
 for tweet in matched_tweets:
     print(tweet.get_text(), "\n")
Sign up to request clarification or add additional context in comments.

1 Comment

This works. However, I think I am needing to return all search results with the search term. This only returns 1 result. I think this may have to do with me setting match = 1. What can I do to fix this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.