TheAlgorithms/Python · info · ValueError

Exactly one argument must be 0

Error message

Exactly one argument must be 0

What it means

The trailing else branch in electrical_impedance() raises 'Exactly one argument must be 0'. It is effectively dead code: the guard `if (resistance, reactance, impedance).count(0) != 1` at the top already returns via ValueError('One and only one argument must be 0') for every input that does not have exactly one zero, so the if/elif chain (resistance==0 / reactance==0 / impedance==0) always matches and this raise can never execute. Its message also differs from the primary guard's message, which is a minor inconsistency.

Source

Thrown at electronics/electrical_impedance.py:40

    >>> electrical_impedance(0,4,5)
    {'resistance': 3.0}
    >>> electrical_impedance(3,0,5)
    {'reactance': 4.0}
    >>> electrical_impedance(3,4,5)
    Traceback (most recent call last):
      ...
    ValueError: One and only one argument must be 0
    """
    if (resistance, reactance, impedance).count(0) != 1:
        raise ValueError("One and only one argument must be 0")
    if resistance == 0:
        return {"resistance": sqrt(pow(impedance, 2) - pow(reactance, 2))}
    elif reactance == 0:
        return {"reactance": sqrt(pow(impedance, 2) - pow(resistance, 2))}
    elif impedance == 0:
        return {"impedance": sqrt(pow(resistance, 2) + pow(reactance, 2))}
    else:
        raise ValueError("Exactly one argument must be 0")


if __name__ == "__main__":
    import doctest

    doctest.testmod()

View on GitHub (pinned to f5988cc097)

Solutions

  1. No action needed for callers; the earlier ValueError('One and only one argument must be 0') is what you will actually see.
  2. If maintaining the file: delete the unreachable else or align its message with the primary guard to remove the inconsistency.
  3. When writing branch-coverage tests, mark this branch as unreachable rather than forcing a test through it.

Example fix

# before
    elif impedance == 0:
        return {"impedance": sqrt(pow(resistance, 2) + pow(reactance, 2))}
    else:
        raise ValueError("Exactly one argument must be 0")

# after
    else:  # impedance == 0 (only remaining case after the count guard)
        return {"impedance": sqrt(pow(resistance, 2) + pow(reactance, 2))}
Defensive patterns

Strategy: validation

Validate before calling

# Guard the real (reachable) error instead:
assert (resistance, reactance, impedance).count(0) == 1

Prevention

When it happens

Trigger: Not reachable through the public API with any float inputs; would only fire if the initial count-based guard were removed or the equality checks were changed (e.g. tolerance-based comparison).

Common situations: Encountered only when reading or refactoring the source, running mutation tests, or writing exhaustive branch-coverage tests; also relevant if a fork removes the first guard and inherits the divergent message.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/9685570b5a459f5f. Report an issue: GitHub.