Skip to content

Instantly share code, notes, and snippets.

@lhsfcboy
Created June 26, 2017 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lhsfcboy/8b05fb9e29f74e9bc4646aff3a5349c7 to your computer and use it in GitHub Desktop.
Save lhsfcboy/8b05fb9e29f74e9bc4646aff3a5349c7 to your computer and use it in GitHub Desktop.
Python中模拟switch-case语句
"""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