AtsushiSakai/PythonRobotics · error · ValueError

WhileDoElseNode must have exactly 3 or 2 children

Error message

WhileDoElseNode must have exactly 3 or 2 children

What it means

WhileDoElseNode.tick requires exactly 2 or 3 children: [condition, do] or [condition, do, else]. Any other count raises this before execution because the node's semantics are undefined otherwise.

Source

Thrown at MissionPlanning/BehaviorTree/behavior_tree.py:198

        If condition fails, executes else node (child[2]) if present and returns result of else node
        If condition fails and there is no else node, returns SUCCESS

    Example:
        .. code-block:: xml

            <WhileDoElse>
                <Condition />
                <Do />
                <Else />
            </WhileDoElse>
    """

    def __init__(self, name):
        super().__init__(name)

    def tick(self) -> Status:
        if len(self.children) != 3 and len(self.children) != 2:
            raise ValueError("WhileDoElseNode must have exactly 3 or 2 children")

        condition_node = self.children[0]
        do_node = self.children[1]
        else_node = self.children[2] if len(self.children) == 3 else None

        condition_status = condition_node.tick_and_set_status()
        if condition_status == Status.SUCCESS:
            do_node.tick_and_set_status()
            return Status.RUNNING
        elif condition_status == Status.FAILURE:
            if else_node is not None:
                else_status = else_node.tick_and_set_status()
                if else_status == Status.SUCCESS:
                    self.reset_children()
                    return Status.SUCCESS
                elif else_status == Status.FAILURE:
                    self.reset_children()
                    return Status.FAILURE

View on GitHub (pinned to 1fe4fb980f)

Solutions

  1. Configure the node with exactly 2 children (condition, do) or 3 children (condition, do, else), in that order.
  2. If you need more branches, use a SelectorNode/SequenceNode combination instead.
  3. Assert the child count after building the tree.

Example fix

// before
wde.add_child(cond); wde.add_child(do); wde.add_child(else_); wde.add_child(extra)

// after
wde.add_child(cond); wde.add_child(do); wde.add_child(else_)
Defensive patterns

Strategy: validation

Validate before calling

assert len(wde.children) in (2, 3), 'WhileDoElseNode needs 2 or 3 children'

Type guard

def valid_wde(node) -> bool:
    return len(node.children) in (2, 3)

Prevention

When it happens

Trigger: Adding 1 child, or 4 or more children, to a WhileDoElseNode and ticking it.

Common situations: Treating WhileDoElseNode like a generic sequence node and appending many children, or forgetting the optional else branch ordering when assembling the tree from XML.

Related errors


AI-assisted analysis of AtsushiSakai/PythonRobotics@1fe4fb980f (2026-08-28). Data as JSON: /api/errors/16faa3a85b598124. Report an issue: GitHub.