Speak If You Can

Python for AIMedium+10 XP+10

Speak If You Can

A toy box holds anything at all. Some things can speak, some can't.

The starter code has Dog (a child of Animal), Robot (no parent), and Rock (no parent, and no speak). Don't change them.

Write safe_chorus(things). For each thing in the list, in order:

  • if it has a speak() method, add what speak() returns,
  • otherwise, add the text "(silent)".

Return the list. An empty list gives [].

Don't decide by family. A Robot isn't an Animal, but it can still speak. Things in the list can also be plain text or numbers.

Examples

things = [Dog("Rex"), Rock("Pebble")]
returns ['Rex says woof!', '(silent)']
things = [Dog("Rex"), Rock("Pebble"), Robot("Unit 7")]
returns ['Rex says woof!', '(silent)', 'Unit 7 says beep!']

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run