vendredi 20 novembre 2020

(Python)How can I make more than one pairs of of if-else(elif) in the same level?

How can I make more than one pairs of of if-else(elif) in the same block? for a simple example,

def printAandB(a,b):
    #function- print a according to its value
    if a==1:
       print("a=1")
    elif a==2:
       print("a=2")


    #function- print b according to its value
    if b==1:
       print("b=1")
    elif b==2:
       print("b=2")
    else:
       print("I don't know")

I hope the each pair of if-else is dealt separately. But logically in this case if-else relationship becomes very wrong, cause it's not clear that from which 'if', 'elif's and'else's were made.

Is there another way to make this work without making separate methods for printing a and b?

Aucun commentaire:

Enregistrer un commentaire