2

I have a twitter bot that responds to tweets containing certain strings from array t. I'm trying to write a conditional statement that restricts it from responding to tweets containing strings from another array, a. In theory it should work but it doesn't. The bot disregards the if/else statement. My code is as follows:

#I search for tweets to my bot's handle
twt = api.search(q='@samplehandle')

#list of specific strings we want to omit from responses
a = ['java',
     'swift']


#list of specific strings I want to check for in tweets and reply to
t = ['I love code',
     'python rocks',
     'javascript']

for c in twt:
    for b in a:
            if b not in c.text:
                for s in twt:
                    for i in t:
                        if i in s.text:
                            sn = s.user.screen_name
                            m = "@%s This is a lovely tweet" % (sn)
                            s = api.update_status(m, s.id)

            else:
                print "Null"

Thank you

3
  • 3
    You are iterating over twt multiple times (nested). Use more descriptive variable names instead of c, b, a, s, and i, and you'll have an easier time determining what is happening and where. May 12, 2016 at 23:50
  • @TigerhawkT3 with the double iteration of twt I assumed I could loop through all twt items (indexing with c first) then running a check if my conditions were met and subsequently responding to each approved tweet (indexing with i). Is this incorrect?
    – babusi
    May 12, 2016 at 23:56
  • You are checking each tweet for each tweet (N^2). Only check each tweet once. Try using operators like and or or to form an expression for the full condition you'd like to check to approve a tweet. May 12, 2016 at 23:57

1 Answer 1

3

Instead of having a ton of nested for loops your program will be much more manageable if you make use of a function to determine if a tweet contains words in a specific list. I also changed your variable names because there's no way to work with a, b, c, d,

#list of specific strings we want to omit from responses
badWords = ['java', 'swift']

#list of specific strings I want to check for in tweets and reply to
goodWords = ['I love code', 'python rocks', 'javascript']


def does_contain_words(tweet, wordsToCheck):
    for word in wordsToCheck:
        if word in tweet:
            return True
    return False

for currentTweet in twt:
    #if the tweet contains a good word and doesn't contain a bad word
    if does_contain_words(currentTweet.text, goodWords) and not does_contain_words(currentTweet.text, badWords):
        #reply to tweet
6
  • Thanks for your help @Racialz, I tried your solution but the console returned the error: if doesContainWords(currentTweet.text, doRespondTo.text) and not doesContainWords(currentTweet.text, dontRespondTo): AttributeError: 'list' object has no attribute 'text'
    – babusi
    May 13, 2016 at 0:10
  • What is twt in your example? I thought it was a list of tweets, each you could get the text by doing .text?
    – Keatinge
    May 13, 2016 at 0:11
  • I'm getting it to work using the exact code I posted. Make sure to include twt=api.search(q="something"). Can you post your full updated code now? Show everything except your twitter api key and tokens.
    – Keatinge
    May 13, 2016 at 0:19
  • Oh great, I hadn't seen your edit. This is perfect. Thank you so much.
    – babusi
    May 13, 2016 at 0:27
  • By the way, one small thing you might want to change is to ignore cases. You might want to do if word.upper() in tweet.upper() to ignore cases. Right now the program will respond to 'python rocks' but not 'Python rocks'
    – Keatinge
    May 13, 2016 at 0:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.