简体   繁体   English

如何在 Python 中使用 if-else 语句在多个变量之间切换

[英]How to switch between multiple variables using an if-else statement in Python

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM