Created
June 26, 2017 05:31
-
-
Save lhsfcboy/8b05fb9e29f74e9bc4646aff3a5349c7 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
"""Python并没有switch-case的语法,等效的用法要么是像上面一样用if-elif-else的组合,要么可以考虑字典""" | |
pets = ['dog', 'cat', 'droid', 'fly'] | |
food_for_pet = { | |
'dog': 'steak', | |
'cat': 'milk', | |
'droid': 'oil', | |
'fly': 'sh*t' | |
} | |
for pet in pets: | |
food = food_for_pet[pet] if pet in food_for_pet else None | |
print(food) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment