Skip to content

Instantly share code, notes, and snippets.

@4nth0
Created February 27, 2020 13:44
Show Gist options
  • Save 4nth0/3c054dc986254c4675c69087979264b7 to your computer and use it in GitHub Desktop.
Save 4nth0/3c054dc986254c4675c69087979264b7 to your computer and use it in GitHub Desktop.
Using dictionary as Switch / Case statement in Python
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