In this tutorial, I am going to show you how to implement Switch Case using Python. Unlike other languages like C, C++ and Java, Python doesn’t have a Switch Case struct. But as you can see in this tutorial, it is possible to implement a structure identical to the Switch Case using Python.
You don’t need to have installed anything more than your Python interpreter.
Code
As you can see in the code below, there is a class called “Switcher”, this class defines your Switch Case, this is where you insert the code you want to execute in each case, as you can see in example code (#comments).
In this example you need to insert the numbers in your python console, if you insert the numbers 1, 2 or 3 in the console, then you can see which case is executed, through the print made by the program. If you insert another number or letter, you see “Invalid Option”, which is the default value. inside the “if method ==” Invalid Option “ you can place any code you want to execute when the chosen option does not exist.
Create a new file “switchCase.py” and copy the code below to your file and save it. Then run the code and see the program’s prints on the console.
# Class of Switch-Case
class Switcher(object):
def service_switcher(self, argument, informations):
"""Dispatch method"""
method_name = 'case_' + str(argument)
# Get the method from 'self'. Default is "Invalid option".
method = getattr(self, method_name, "Invalid option")
if method == "Invalid option":
return method
# Call the method as we return it
return method(method_name)
def case_1(self, information):
# here you can insert your case code
return information
def case_2(self, information):
# here you can insert your case code
return information
def case_3(self, information):
# here you can insert your case code
return information
a = Switcher()
information = "Init program"
while True:
## Get input (case choice) ###
choice = input('Enter your choice [1-3] : ')
information = a.service_switcher(choice, information)
print(information)
Your Result:
Thank you for every other informative site. The place else could I get that type of
information written in such a perfect way?
I have a venture that I’m just now operating on, and I have been on the glance out for such information.
Thanks for your comment.
Simply desire to say your article is as amazing.
The clarity in your put up is simply great and i could think you
are an expert in this subject. Well with your permission let me to snatch your feed
to stay updated with approaching post. Thanks one million and please continue the
gratifying work.
I like the valuable information you provide in your articles.
I’ll bookmark your blog and check again here regularly.
I am quite certain I’ll learn a lot of new stuff
right here! Good luck for the next!
Its like you read my mind! You appear to know a lot about this,
like you wrote the book in it or something. I think that you can do with a few pics to drive the
message home a bit, but other than that, this is fantastic blog.
A great read. I’ll definitely be back.
Hi to every one, it’s genuinely a pleasant for me to go to see this web site, it includes priceless Information.
Every weekend i used to pay a visit this site, because i want enjoyment,
for the reason that this this web page conations genuinely nice funny
data too.
Very nice post. I simply stumbled upon your blog and wished to say that I have truly loved browsing your blog posts.
After all I’ll be subscribing for your feed and I’m hoping you write once more very soon!
Very nice post. I just stumbled upon your weblog and wanted to say that I’ve truly enjoyed surfing around your
blog posts. After all I’ll be subscribing to your feed
and I hope you write again very soon!
If you would like to grow your knowledge simply keep visiting this web site and be updated with the most recent information posted here.