9/27
Part 2 of 7 · Count with messagesLearn and do

The root keeps the answer

The root is the one machine nobody asked. The test calls start() on it instead of sending an ask. start does what any machine does on an ask, with no parent: self.ask_children(parent=None).

The difference comes at the end. When the root is done, there's nobody to send the total to. So finish_if_done asks one question: is self.parent None? If it is, store the total in self.count. If not, send it to the parent.

Two cases to check before you call it done:

  • A root with no children. start asks nobody, and ask_children calls finish_if_done at once, which sets count to 1 and sends nothing. If your start sends the asks without checking, count stays None forever.
  • A root whose id isn't 0. Nothing in the code may assume the root's id. The parent being None is what makes a machine the root.

The editor holds the whole level 1 Machine except two lines. Write start, and finish finish_if_done so the root stores self.count and every other machine sends its total to its parent.