Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,166 questions

40,722 answers

573 users

How to implement the switch statement using dictionary in Python

1 Answer

0 votes
def function1():
    print("function1")
def function2():
    print("function2")
def function3():
    print("function3")
def function4():
    print("function4")    

switch = {1:function1, 2:function2, 3:function3, 4:function4}

a = 1
switch.get(a, function1)()

switch.get(3, function1)()




'''
run:

function1
function3

'''

 





answered Apr 29, 2021 by avibootz

Related questions

1 answer 24 views
1 answer 24 views
4 answers 89 views
2 answers 27 views
1 answer 45 views
...