mardi 8 juin 2021

How can I avoid a lot of if's and elif's in the options menu

I want to build an options menu where the user types a number and according to the number he calls a function that does something .. For example:

def func1():
    pass
def func2():
    pass
def func3():
    pass

user_input = int(input("enter option: "))
if user_input == 1:
    func1()
elif user_input == 2:
    func2()
elif user_input == 3:
    func3()

So instead, build something in a few lines of code that will work in the same way and of course also on a function that gets parameters

Aucun commentaire:

Enregistrer un commentaire