livepython.net

188. Python Program to Make Animal Classes Using Inheritance

MediumInheritance

Overview

One parent class, two children that override a method, and one __str__ that works for all three.

Problem Statement

Define a parent class Animal with a name, a speak() method that returns makes a sound, and a __str__ method that returns the name, a space, then what speak() returns. Then define two child classes of Animal:

  • Dog, whose speak() returns says Woof
  • Cat, whose speak() returns says Meow

The children only override speak(): they get __init__ and __str__ from Animal.

Read the number of animals, then one animal per line: its kind and its name. Make a Dog for dog, a Cat for cat, and a plain Animal for any other kind. Keep them in a list, then print every animal in the list.

Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.

Input

The number of animals, then one animal per line. Example:

3
dog Rocky
cat Lucky
cow Sunny

Output

The output for that example:

Rocky says Woof
Lucky says Meow
Sunny makes a sound

The tests try 3 different inputs.

Learn this firstChapter 20 · Python Inheritance: Parent and Child Classes
solution.py
# write your code here

EXPECTED OUTPUT

1Rocky says Woof
2Lucky says Meow
3Sunny makes a sound

YOUR OUTPUT

Press Run to see your output here. Lines are compared with the expected output.