Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # fn_switchcase.py
- def cat_sound():
- return "Meow"
- def dog_sound():
- return "Woof"
- def cow_sound():
- return "Moo"
- def unknown_sound():
- return "*** Unlisted ***"
- def animal_sound(animal):
- sounds = {
- "cat": cat_sound,
- "dog": dog_sound,
- "cow": cow_sound
- }
- return sounds.get(animal, unknown_sound)()
- # Test the function
- for animal in 'cat dog cow sloth'.split():
- print(animal_sound(animal))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement