0

How would I make a switch between multiple variables using a single switch variable?

Update: To clarify the intent is to switch back between these two sets of variables an indefinite amount of times.

When I try this I get the following error.

a1= 'process1'
a2 = 'process2'

b1 = 'action1'
b2 = 'action2'

switch = True # the switch to indicate which set of variables to use
N = 10        # the number of times to switch between the two sets of variables

# alternate between two sets of variables N times
for i in range (N):
    active_process, active_action = a1, b1 if switch else a2, b2

    print("active_process: %s, active_action is: %s" %(active_process, active_action))
    switch = not switch

Traceback:

Traceback (most recent call last):
  File "/home/username/.PyCharm2019.3/config/scratches/scratch_10.py", line 10, in <module>
    active_process, active_action = a1, b1 if switch else a2, b2
ValueError: too many values to unpack (expected 2)

Process finished with exit code 1
7
  • 1
    You don't need i or switch here: for response, greeting in (a1, b1), (a2, b2):. Commented Jan 31, 2020 at 22:47
  • 1
    Or perhaps more idiomatic: responses = ['yes', 'no']; greetings = ['hello', 'goodby']; for response, greeting in zip(responses, greetings): Commented Jan 31, 2020 at 22:48
  • Thank you for your help. I updated to make more clear the intent of the question. There was a switch between the two sets of variables an indefinite amount of time. Commented Jan 31, 2020 at 23:33
  • 1
    I'd write that as for ap, aa in islice(cycle([(a1,b1), (a2,b2)]), N): (where islice and cycle are imported from itertools). Or if you need/want i, for i, (ap, aa) in zip(range(N), cycle(...)). Commented Feb 1, 2020 at 1:32
  • The error comes from precedence issues: (a1, b1) if switch else (a2, b2). Commented Feb 1, 2020 at 1:33

2 Answers 2

1

You're making this far too fragile. You have a table of greeting/response values and a Boolean that tell you which to use. Just do this with a direct-access list:

table = [ ("process1", "action1"),
          ("process2" , "action2")
        ]

N = 10
for i in range(10):
    print("%s, the answer is: %s" % table[i %2])

Alternately, use a dict:

table = { True:  ("process1", "action1"),
          False: ("process2" , "action2")
        }
N = 10
for i in range(N):
    print("%s, the answer is: %s" % table[i %2])
Sign up to request clarification or add additional context in comments.

4 Comments

The intent was to switch between the two sets of variables an indefinite amount of times. This is easily accomplished if you take the modulo i%2 to reference the index of the table list.
Yes, it is. I answered the posted question; you updated afterward.
The only change was to update the variables so that they were not quite as misleading as to what the original intent of the question was and to change the number of times to alternate from 2 to 10. Do you mind if I update your answer with the i%2 as the index of table?
Thank you @Prune I don't want to seem that I'm not appreciative of you. You're right a table is more robust!
0

Using a list to pack and unpack the items gave the desired outcome:

a1= 'process1'
a2 = 'process2'

b1 = 'action1'
b2 = 'action2'

switch = True # the switch to indicate which set of variables to use
N = 10        # the number of times to switch between the two sets of 

# alternate between two sets of variables N times
for i in range (N):
    [active_process, active_action] = [a1, b1] if switch else [a2, b2]

    print("active_process: %s, active_action is: %s" %(active_process, active_action))
    switch = not switch

Output:

active_process: process1, active_action is: action1
active_process: process2, active_action is: action2
active_process: process1, active_action is: action1
active_process: process2, active_action is: action2
active_process: process1, active_action is: action1
active_process: process2, active_action is: action2
active_process: process1, active_action is: action1
active_process: process2, active_action is: action2
active_process: process1, active_action is: action1
active_process: process2, active_action is: action2

Process finished with exit code 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.