Struggling with a series of variables

情到浓时终转凉″ 提交于 2019-12-12 04:46:59

问题


To avoid tweets becoming caught in the twitter spam filter I have some code that goes to tinyurl and creates a new short url each time the code is run for each of the original urls. What i want is each time 'u' is printed, it's value should be passed to a variable 'linkvar1', 'linkvar2', 'linkvar3' etc. This is the passed to the tweet submission later in the code:

import simplejson
import httplib2
import twitter
import tinyurl

print("Python will now attempt to submit tweets to twitter...")

try:

    api = twitter.Api(consumer_key='',
                      consumer_secret='',
                      access_token_key='',
                      access_token_secret='')

    for u in tinyurl.create('http://audiotechracy.blogspot.co.uk/2014/03/reviewing-synapse-antidote-rack.html',
                        'http://audiotechracy.blogspot.co.uk/2014/03/free-guitar-patches-for-propellerhead.html',
                        'http://audiotechracy.blogspot.co.uk/2014/02/get-free-propellerhead-rock-and-metal.html',
                        ):
        print u
        linkvar1 = u
        linkvar2 = u
        linkvar3 = u

    status = api.PostUpdate("The new Synapse Antidote Rack Extension:" + linkvar1 + " #propellerhead #synapse")
status = api.PostUpdate("Free Propellerhead guitar patches for everyone!" + linkvar2 + " #propellerhead #reason #guitar")
status = api.PostUpdate("Free Metal and Rock drum samples!" + linkvar3 + " #propellerhead #reason)


print("Tweets submitted successfully!")

except Exception,e: print str(e)
print("Twitter submissions have failed!!!")

However at the minute all this does is use the tinyurl generated last for all tweets submitted. I'm sure this is an easy fix that I'm just being stupid about, but does anyone know how to do what I want?

Thanks


回答1:


Your issue is that you aren't doing anything with your linkvar variables through each loop. Thus, each time the loop runs, they are getting overwritten.

You have a few options

Option 1: Make the linkvars a list that you append to each loop

linkvar1 = []
for u in ...
   ...
   linkvar1.append(u)

# Post to twitter
for p in linkvar1:
    status = api.PostUpdate("The new Synapse Antidote Rack Extension:" + p + " #propellerhead #synapse")
    status = api.PostUpdate("Free Propellerhead guitar patches for everyone!" + p + " #propellerhead #reason #guitar")
    status = api.PostUpdate("Free Metal and Rock drum samples!" + p + " #propellerhead #reason)

At the end of your first for loop, you will have values in the linkvar variable. I am not sure why you are using three, do I chopped it down to just the single instance. You can then loop through using another for loop, or passed whole-sale to your own function that will handle them appropriately. In either case, all of your urls are now in a list in each of these variables

Option 2: Call a function to perform on each loop

for u in ...
   ...
   MyTwitterFunction(u)

def MyTwitterFunction(url):
    status = api.PostUpdate("The new Synapse Antidote Rack Extension:" + url + " #propellerhead #synapse")
    status = api.PostUpdate("Free Propellerhead guitar patches for everyone!" + url + " #propellerhead #reason #guitar")
    status = api.PostUpdate("Free Metal and Rock drum samples!" + url + " #propellerhead #reason)

Each time the loop is iterated over, the MyTwitterFunction will be called with the value of u.

Option 3: Pull your posting code directly into your for loop

for u in ...
    status = api.PostUpdate("The new Synapse Antidote Rack Extension:" + u + " #propellerhead #synapse")
    status = api.PostUpdate("Free Propellerhead guitar patches for everyone!" + u + " #propellerhead #reason #guitar")
    status = api.PostUpdate("Free Metal and Rock drum samples!" + u + " #propellerhead #reason)

This eliminates the need for the linkvar variables and the extra for loop. You can post directly from the loop in which the URLs are created.




回答2:


I'm not certain what you mean by "passed to a variable". It looks as though you're assigning the value of u to each of your 3 variables and then over-writing it - e.g.:

 for x in range(5):
   y = x

Will result in the value of 4 being assigned to y. Did you maybe want to make a list? For example:

y = []
for x in range(5):
  y.append(x)

which will result in

y = [0,1,2,3,4]

I think this is what you're aiming for with your link1,2,3 variables.



来源:https://stackoverflow.com/questions/22898046/struggling-with-a-series-of-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!