Created
February 27, 2020 13:44
-
-
Save 4nth0/3c054dc986254c4675c69087979264b7 to your computer and use it in GitHub Desktop.
Using dictionary as Switch / Case statement in Python
This file contains hidden or 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
statusMessages = { | |
0: "Status is pending", | |
1: "Status is active", | |
2: "Status is cancelled", | |
"default": "Status is unknown" | |
} | |
def print_status_message(status) : | |
if status in statusMessages: | |
print(statusMessages[status]) | |
else: | |
print(statusMessages["default"]) | |
print_status_message(0) # "Status is pending" | |
print_status_message(9) # "Status is unknown" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment