Advertisement
here2share

# fn_switchcase.py

May 5th, 2020 (edited)
1,328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.46 KB | None | 0 0
  1. # fn_switchcase.py
  2.  
  3. def cat_sound():
  4.     return "Meow"
  5.  
  6. def dog_sound():
  7.     return "Woof"
  8.  
  9. def cow_sound():
  10.     return "Moo"
  11.  
  12. def unknown_sound():
  13.     return "*** Unlisted ***"
  14.  
  15. def animal_sound(animal):
  16.     sounds = {
  17.         "cat": cat_sound,
  18.         "dog": dog_sound,
  19.         "cow": cow_sound
  20.     }
  21.     return sounds.get(animal, unknown_sound)()
  22.  
  23. # Test the function
  24. for animal in 'cat dog cow sloth'.split():
  25.     print(animal_sound(animal))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement