vendredi 31 janvier 2020

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? When I try this I get the following error.

a1= 'yes'
a2 = 'no'

b1 = 'hello'
b2 = 'goodbye'

switch = True

for i in range (2):
    response, greeting = a1, b1 if switch else a2, b2

    print("%s, the answer is: %s" %(greeting, response))
    switch = not switch

Traceback:

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

Process finished with exit code 1

Aucun commentaire:

Enregistrer un commentaire