Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (47.6k points)

I'd like to know, is there a Python equivalent for the case statement such as the examples available on VB.net or C#?

1 Answer

0 votes
by (106k points)
edited by

Actually, there is no switch statement in Python but you can write your program using a dictionary that will work as a switch statement.

Example code for you:

# Firstly you can define the number of function blocks as many options you want.

def zero():

print("You have selected option A.\n")

def one():

print("You have selected option B.\n")

def two():

print("You have selected option C.\n")

def three():

print("You have selected option D.\n") 

# Now you need to create a map the inputs to the function blocks 

options = {0 : zero, 1 : sqr, 4 : sqr, 9 : sqr, 2 : even, 3 : prime, 5 : prime, 7 : prime, }

options[num]() #This statement is for calling your options

To know more about this you can have a look at the following video tutorial:-

Related questions

+1 vote
2 answers
0 votes
1 answer
asked Dec 22, 2020 in SQL by Appu (6.1k points)
0 votes
1 answer
0 votes
1 answer
asked Sep 29, 2019 in Java by Shubham (3.9k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...