Created
December 11, 2020 08:05
-
-
Save funkycoder/0a3a14d3bf2629642bacf244a91fecd2 to your computer and use it in GitHub Desktop.
Switch 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
# Python doesnt have switch case statement. Hereis the workaround | |
def grading(score): | |
switcher = { | |
0: "Zero", | |
1: "One", | |
2: "Two", | |
3: "Three", | |
4: "Four", | |
} | |
# get() method of dictionary data type returns | |
# value of passed argument if it is present | |
# in dictionary otherwise second argument will | |
# be assigned as default value of passed argument | |
return switcher.get(score, "Five and above") | |
print(grading(5)) | |
>>>Five and above |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment