Skip to content Skip to sidebar Skip to footer

Struggling With A Series Of Variables

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

Solution 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)

defMyTwitterFunction(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

foruin ...
    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.

Solution 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.

Post a Comment for "Struggling With A Series Of Variables"