Skip to content

Instantly share code, notes, and snippets.

@SeanSyue
Created August 12, 2018 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SeanSyue/76fa80d96f91af6c12a28b2aa65d6dff to your computer and use it in GitHub Desktop.
Save SeanSyue/76fa80d96f91af6c12a28b2aa65d6dff to your computer and use it in GitHub Desktop.
Implement switch functionality in Python.
# 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