Cats and dogs

Report a typo

Now let's practice with using custom data structures and user-defined types with pattern matching. Your task is to write such patterns in each of the case statements, so that they behave according to what's written in print functions. Don't forget to add annotations to Cat and Dog classes.

Please do not call the match_animal function, it will be called automatically.

Sample Input 1:

Cat Kitty 5 siamese 19

Sample Output 1:

Any cat named Kitty of age 5 matches this case! Breed is siamese, tail length is 19.0
Write a program in Python 3
# TODO import annotation

# TODO add annotation
class Cat:
name: str
age: int
breed: str
tail_length: float

class Dog:
name: str
age: int
breed: str
barking_volume: float
is_hunting_breed: bool

def match_animal(animal):
# Declare a match statement for the passed animal
case # TODO
print('Any cat with the tail length of 20.0 matches this case! '
f'Name is {name}, age is {age}, breed is {breed}')
case # TODO
print('Any cat named Kitty of age 5 matches this case! '
f'Breed is {breed}, tail length is {tail_length}')
case # TODO
print('Any dog named Fluffy matches this case! No need for statistics :)')
case # TODO
print('Any dog matches this case!')
case # TODO
print('Every left animal matches this case')

Create a free account to access the full topic