mardi 1 décembre 2020

Better way to write if-elif-elif-elif-else based on a dictionary?

How can I improve (reduce the writing) for this if-else block to run a method based on an option entered? I've tried looking at SO and various other areas for solutions but didn't find something fit my problem.

def funcA(arg1, arg2):
    ...

def funcB(arg1):
    ...

def funcC():
    ...

def funcD():
    ...

def funcE():
    ...

if option == 1:
   funcA(arg1, arg2)
elif option == 2:
   funcB(arg1)
elif option == 3:
   funcC()
elif option == 4:
   funcD()
elif option == 5:
   funcE()
else:
   pass

Apologies if the code is not clear.

Aucun commentaire:

Enregistrer un commentaire