Mike
Mike

Reputation: 1

how to use the variable outside of the loop

I'm working on my python script as I'm created the variable outside of the loop so I can generate the list of numbers. I have a problem with generating the list of numbers.

When I try this:

def All_Channels(self):
     #Pull the data from the database
     channelList = list()
     database_path = 
xbmc.translatePath(os.path.join('special://userdata/addon_data/script.tvguide', 
'source.db'))

     if os.path.exists(database_path):
         #get the channels list
         cur.execute('SELECT channel FROM programs WHERE channel GROUP BY 
channel')
         for row in cur:
             channels = row[0].encode('ascii')
             channelList.append(channels)

             # set the channels text
             for index in range(0, CHANNELS_PER_PAGE):
                 channel = channelList[index]
                 channel_index = index

                  if channel is not None:
                      for channels_id in range(4127, 4547, 70):
                          for channels_start_end in range(69): 
                              ID = channels_id + channels_start_end
                          print ID

I will get the output like this:

21:35:54 T:4724  NOTICE: 4545
21:35:54 T:4724  NOTICE: 4545
21:35:54 T:4724  NOTICE: 4545
21:35:54 T:4724  NOTICE: 4545
21:35:54 T:4724  NOTICE: 4545
21:35:54 T:4724  NOTICE: 4545
21:35:54 T:4724  NOTICE: 4545

The output should be like this:

I will get the output like this:

21:35:54 T:4724  NOTICE: 4127
21:35:54 T:4724  NOTICE: 4197
21:35:54 T:4724  NOTICE: 4267
21:35:54 T:4724  NOTICE: 4337
21:35:54 T:4724  NOTICE: 4407
21:35:54 T:4724  NOTICE: 4477
21:35:54 T:4724  NOTICE: 4547

The problem are lie in this code:

for channels_id in range(4127, 4547, 70):
    for channels_start_end in range(69): 
        ID = channels_id + channels_start_end
     print ID

Can you please tell me how I can generate the numbers I want outside of the loop and how I can use the variable outside of the loop?

EDIT: When I try this:

for channels_id in range(4127, 4547, 70):
    print channels_id

I will get this:

00:30:28 T:6972  NOTICE: 4127
00:30:28 T:6972  NOTICE: 4197
00:30:28 T:6972  NOTICE: 4267
00:30:28 T:6972  NOTICE: 4337
00:30:28 T:6972  NOTICE: 4407
00:30:28 T:6972  NOTICE: 4477

There are one value that are missing which it is 4547.

EDIT: *Update 1:

When I try to use this:

# set the channels text
for index in range(0, CHANNELS_PER_PAGE):
    channel = channelList[index]
    channel_index = index

    for channels_id in range(4127, 4548, 70):
        ID = []
        if channel is not None:
           ID = channels_id
    print ID
    print channel
    self.getControl(ID).setLabel(channel)

Output:

01:59:10 T:6768  NOTICE: 4547
01:59:10 T:6768  NOTICE: 4547
01:59:10 T:6768  NOTICE: 4547
01:59:10 T:6768  NOTICE: 4547
01:59:10 T:6768  NOTICE: 4547
01:59:10 T:6768  NOTICE: 4547
01:59:10 T:6768  NOTICE: 4547

It will not store the value outside of the loop as it will get the same value 7 in row.

I want to use the loop of for channels_id under the for index loop so I can then generating the list of values before I can use the control self.getControl(ID).setLabel(channel) to set the label using the variable channel for each value that I set in the label when I generating the values using the variable ID. I've been asking you how I can store the values outside of the loop for later use. If you can help, please update your code in your answer post.

Upvotes: 0

Views: 6497

Answers (1)

abarnert
abarnert

Reputation: 365717

You already can access the variable outside the loop. That's why it's printing out 4545, instead of giving you an UnboundLocalError or other exception.

The problem is that "outside the loop" only happens once, with the last value from the loop. So, you set ID, then set it again, then set it again, 69 times, and then finally print the 69th value. If you want to print each of the 69 values, you have to do it each time through the loop. Instead of this:

for channels_start_end in range(69): 
    ID = channels_id + channels_start_end
print ID

Do this:

for channels_start_end in range(69): 
    ID = channels_id + channels_start_end
    print ID

Or, if you want to do something with all 69 values, you have to accumulate them all, e.g., by adding them to a list, so all 69 of them are available, instead of just the last one:

IDs = []
for channels_start_end in range(69): 
    IDs.append(channels_id + channels_start_end)
print IDs

From your comments, it seems like your problem is even simpler. You don't want to print out all the values, only every 70th value. You're already got every 70th value in channels_id. So, just print that:

for channels_id in range(4127, 4547, 70):
    for channels_start_end in range(69): 
        ID = channels_id + channels_start_end
    print channels_id

But really, you don't seem to be doing anything with that ID value, or doing anything useful with the inner loop at all; instead of generating and then ignoring 69 out of every 70 values, why not just not generate them in the first place? Like this:

for channels_id in range(4127, 4547, 70):
    print channels_id

Your last problem is that the loop doesn't include 4547.

That's because a Python range only includes values less than the stop value, not less than or equal to it. For a simpler example:

>>> for i in range(0, 3, 1):
...     print i
0
1
2

Notice that it didn't print 3.

So, you could use range(4127, 4548, 70) or range(4127, 4617, 70) (it's hard to know which one is more "natural" in your use case without understanding what that use case is) to get all the values you want.

Upvotes: 3

Related Questions