TheAlgorithms/Python · error · ValueError

One and only one argument must be 0

Error message

One and only one argument must be 0

What it means

Thrown by electrical_impedance() in electronics/electrical_impedance.py when the number of arguments equal to 0 among (resistance, reactance, impedance) is not exactly one. The function uses the 0-valued argument as the 'unknown' to solve for via the Pythagorean relation Z^2 = R^2 + X^2, so exactly one argument must be 0 as the sentinel for the missing quantity.

Source

Thrown at electronics/electrical_impedance.py:32

) -> dict[str, float]:
    """
    Apply Electrical Impedance formula, on any two given electrical values,
    which can be resistance, reactance, and impedance, and then in a Python dict
    return name/value pair of the zero value.

    >>> electrical_impedance(3,4,0)
    {'impedance': 5.0}
    >>> 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. Set exactly one of resistance/reactance/impedance to 0 to indicate the unknown: electrical_impedance(3, 4, 0) -> {'impedance': 5.0}.
  2. If all three values are known, do not call the function — it is a solver, not a checker.
  3. Check (resistance, reactance, impedance).count(0) == 1 before calling in dynamic code.

Example fix

# before
electrical_impedance(3, 4, 5)  # ValueError: One and only one argument must be 0

# after
z = electrical_impedance(3, 4, 0)  # solve for impedance -> 5.0
Defensive patterns

Strategy: validation

Validate before calling

if (resistance, reactance, impedance).count(0) != 1:
    raise ValueError('exactly one of resistance/reactance/impedance must be 0')

Type guard

def has_single_unknown(r: float, x: float, z: float) -> bool:
    return (r, x, z).count(0) == 1

Try / catch

try:
    result = electrical_impedance(r, x, z)
except ValueError as exc:
    if 'must be 0' in str(exc):
        # caller supplied wrong sentinel pattern
        ...
    raise

Prevention

When it happens

Trigger: electrical_impedance(3, 4, 5) (no argument is 0, nothing to solve for); electrical_impedance(0, 0, 5) or electrical_impedance(0, 4, 0) (two or more zeros, ambiguous/underdetermined).

Common situations: Treating the function as a general validator and passing all three measured values; using None or -1 instead of 0 as the 'unknown' placeholder; copying example calls that omit the sentinel zero.

Related errors


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