Created
August 19, 2014 09:47
-
-
Save gurel/ad14f4cd4b4bb43c06b8 to your computer and use it in GitHub Desktop.
Python Switch Case
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
class switch(object): | |
value = None | |
def __new__(class_, value): | |
class_.value = value | |
return True | |
def case(*args): | |
return any((arg == switch.value for arg in args)) | |
Usage: | |
while switch(n): | |
if case(0): | |
print "You typed zero." | |
break | |
if case(1, 4, 9): | |
print "n is a perfect square." | |
break | |
if case(2): | |
print "n is an even number." | |
if case(2, 3, 5, 7): | |
print "n is a prime number." | |
break | |
if case(6, 8): | |
print "n is an even number." | |
break | |
print "Only single-digit numbers are allowed." | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment