Forums

get the list of values outside of loop

Hi guys,

I'm created the variables in the loops to generating the list of values and channels, I want to use the variable outside of the loop so I can get the list of values. I have a problem with get the list of values.

When I try 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)

I will get the output like this:

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 should be like this:

01:59:10 T:6768  NOTICE: 4127
01:59:10 T:6768  NOTICE: 4197
01:59:10 T:6768  NOTICE: 4267
01:59:10 T:6768  NOTICE: 4337
01:59:10 T:6768  NOTICE: 4407
01:59:10 T:6768  NOTICE: 4477
01:59:10 T:6768  NOTICE: 4547

The problem are lie in this code:

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

I'm stored the variable ID under the loop called for channels_id to generating the list of values. I want to use that variable outside of the loop so I can use the getControl method to get the list of values while I can use the other variable channel to get the list of channels.

Can you please show me how I can use the variable ID outside of the loop where I can use with the other variable channel so I can use it on getControl method?

Hi there -- I'm sorry, I really can't work out what you're trying to do. Could you explain it in other words?

I don't know what to say in other words.

All of my words are already in my first post which it make sense.

Why can't you read it again?

In other words, I want to store the variable ID outside of the loop so I can print the list of values.

This one will only works when I use the print in the loop:

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

How I can store the variable outside of the loop to allow me to print the list of values outside from the loop?

What do you mean by "store the variable ID outside the loop"? That really doesn't make sense to me.

i have told you what i want to do. I want to use the variable outside of the loop so i can print the list of values.

When I try this:

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

I will get the list of values like this:

01:59:10 T:6768  NOTICE: 4127
01:59:10 T:6768  NOTICE: 4197
01:59:10 T:6768  NOTICE: 4267
01:59:10 T:6768  NOTICE: 4337
01:59:10 T:6768  NOTICE: 4407
01:59:10 T:6768  NOTICE: 4477
01:59:10 T:6768  NOTICE: 4547

So when I try this:

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

I will get this:

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 print one value which it should print 7 different values.

Do you know how I can use the variable outside of the loop to get 7 values?

I'm still really perplexed about what you mean. Are you trying to make the variable ID a list so that you can store multiple values in it? If so, this is what you'd write:

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

But I think you should probably work your way through a proper Python tutorial, because nothing's really going to make sense to you until you've learnt some of the basics of the language. I recommend Learn Python the hard way -- despite its name, it's an excellent beginner's guide.

Then you are stupid enough for not taking your own time to read my post. I have told you many times and I am not going to say it over again. I'm trying to make the variable ID a list of values where I'm generating the value from range 4127 to 4547 with 70 each of value, so that I can use that variable ID outside of the for channels_id loop to get it to work with other variable channel.

So in that way I can use that variables, like this:

self.getControl(ID).setLabel(channel)

I hope it will make sense what I'm trying to achieve?

As for your code, it will print the list of 7 same values where you can see it:

18:42:58 T:6884  NOTICE: [4127, 4197, 4267, 4337, 4407, 4477, 4547]
18:42:58 T:6884  NOTICE: [4127, 4197, 4267, 4337, 4407, 4477, 4547]
18:42:58 T:6884  NOTICE: [4127, 4197, 4267, 4337, 4407, 4477, 4547]
18:42:58 T:6884  NOTICE: [4127, 4197, 4267, 4337, 4407, 4477, 4547]
18:42:58 T:6884  NOTICE: [4127, 4197, 4267, 4337, 4407, 4477, 4547]
18:42:58 T:6884  NOTICE: [4127, 4197, 4267, 4337, 4407, 4477, 4547]
18:42:58 T:6884  NOTICE: [4127, 4197, 4267, 4337, 4407, 4477, 4547]

It should like this:

01:59:10 T:6768  NOTICE: 4127
01:59:10 T:6768  NOTICE: 4197
01:59:10 T:6768  NOTICE: 4267
01:59:10 T:6768  NOTICE: 4337
01:59:10 T:6768  NOTICE: 4407
01:59:10 T:6768  NOTICE: 4477
01:59:10 T:6768  NOTICE: 4547

I want to print one value at a time with each different value when I use the code under the for index loop.

I hope this will make sense?

OK, a brief suggestion -- calling people who are trying to help you solve your problem is a really bad idea. Because then they will stop trying to help you.

I recommend you read the book I linked to. Then you should be able to solve the problem yourself.

You don't have a clue what I have already told you. I have told you in many times and you haven't been helping to yourself. I'm using the loop for index in range to get the range of channels 0 to 7 from sqlite3 database. I want to loop all channel ID's that I want [4127 4197 4267 4337 4407 4477 4547] under for index in range loop so i can set the id and channel in the self.getControl. I hope it make sense?

Don't tell me what to do to read the book. I recommend you to buy the book as you still don't understand what I actually want and come back to me when you get it.

@chris0147 -- I want to quote back some of the things you've said so far:

"I don't know what to say in other words. All of my words are already in my first post which it make sense. Why can't you read it again?"

"i have told you what i want to do."

"Then you are stupid enough for not taking your own time to read my post. I have told you many times and I am not going to say it over again"

"Don't tell me what to do to read the book. I recommend you to buy the book as you still don't understand what I actually want and come back to me when you get it."

Now I don't know where you're from, there may be a cultural or language difference, but I think almost anyone in the world would say that you have been rude and impatient.

Nobody here owes you anything. I don't think any of the community of users of PythonAnywhere would be inclined to help someone so rude, and even Giles and I, who are paid to help our users, aren't obliged to deal with anyone that's being obnoxious. Even if you were actually paying for the service, which you're not, we're only really here to help with problems using PythonAnywhere itself, not basic programming questions.

In short, if you want free help with your problems, then you have to make someone want to help you. You could try apologising and explaining yourself again. Alternatively, you can try actually paying someone -- we sometimes recommend codementor, but even they might decide that working with someone rude is not worth the money.