Created
August 12, 2018 02:50
-
-
Save SeanSyue/76fa80d96f91af6c12a28b2aa65d6dff to your computer and use it in GitHub Desktop.
Implement switch functionality in Python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://bytebaker.com/2008/11/03/switch-case-statement-in-python/ | |
def switch_(index_): | |
def zero(): | |
print("You typed zero.\n") | |
def sqr(): | |
print("n is a perfect square\n") | |
def even(): | |
print("n is an even number\n") | |
def prime(): | |
print("n is a prime number\n") | |
options = {0: zero, | |
1: sqr, | |
4: sqr, | |
9: sqr, | |
2: even, | |
3: prime, | |
5: prime, | |
7: prime, | |
} | |
return options[index_]() | |
switch_(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment