switch statements | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

switch statements

can you do switch statements in python, like you can in c++?

2nd Jul 2018, 1:21 AM
Large Toad
Large Toad - avatar
3 Answers
+ 17
No you can not. But you can implement it by yourself. We can create a dictionary where the key is the case value and the key value is a function. def default(): print("default function") def first(): print("first function") def second(): print("second function") dict = { "first": first, "second": second } def switch(case): if case in dict: dict[case]() else: default() #Usage string = input() switch(string)
2nd Jul 2018, 1:28 AM
Toni Isotalo
Toni Isotalo - avatar
+ 3
thank you for clearing that up
2nd Jul 2018, 1:30 AM
Large Toad
Large Toad - avatar
+ 2
new concept wow
2nd Jul 2018, 6:34 PM
Programmer Rushikesh
Programmer Rushikesh - avatar