-3

Just a real quick one. I has a list of variables which only change by 1 digit. I want my if statement to refer to each individual variable, but I'm not sure how to go about it. Can you help me out?

CheckGabba1 = IntVar()
CheckGabba2 = IntVar()
CheckGabba3 = IntVar()
## etc. (x10)

## There are items in these lists (This is just to show that they are lists)
AllEventsGabba = []
SelectedEventsGabba = []

NumEvents = 10
ListIndex = 0

for EventCheck in range(NumEvents):
   if (CheckGabba(ListIndex + 1)).get() == 1:
      SelectedEventsGabba.append(AllEventsGabba[ListIndex])
      ListIndex += 1
   else:
      ListIndex += 1

Obviously (CheckGabba(ListIndex + 1) is wrong, but I'm not sure what it needs to be replaced with to get the loop to autonomously check each variable, rather than hard-writing (Which I can do, but would prefer not to).

ZooKeeper
  • 1
  • 5

2 Answers2

0

I'm not sure if I completely understood your question, but to me it looks something like this:

# Create a dict of the variables that you want to check later
CheckGabba = {i: IntVar() for i in range(10)}

# Then use a list comprehension to filter the data
SelectedEventsGabba = [event for event in CheckGabba.items() if event == 1]

Dov Rine
  • 810
  • 6
  • 12
-1

You could group all CheckGabbas in a List and loop over them, like

check_list=[CheckGabba1,CheckGabba2,CheckGabba3]
for check in check_list:
    if check.get()==1:

an alternative would be to use exec on the string, like

exec "CheckGabba("+str(Listindex+1)+")"
benito_h
  • 460
  • 5
  • 16