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 ind_reactance() in electronics/ind_reactance.py when the count of zero arguments among (inductance, frequency, reactance) is not exactly one. The function solves X_L = 2*pi*f*L for whichever quantity is 0, so 0 acts as the sentinel meaning 'this is the unknown'; supplying all non-zero values or more than one zero is ambiguous.

Source

Thrown at electronics/ind_reactance.py:49

    >>> ind_reactance(35e-6, 0, -1)
    Traceback (most recent call last):
        ...
    ValueError: Inductive reactance cannot be negative

    >>> ind_reactance(0, 10e3, 50)
    {'inductance': 0.0007957747154594767}

    >>> ind_reactance(35e-3, 0, 50)
    {'frequency': 227.36420441699332}

    >>> ind_reactance(35e-6, 1e3, 0)
    {'reactance': 0.2199114857512855}

    """

    if (inductance, frequency, reactance).count(0) != 1:
        raise ValueError("One and only one argument must be 0")
    if inductance < 0:
        raise ValueError("Inductance cannot be negative")
    if frequency < 0:
        raise ValueError("Frequency cannot be negative")
    if reactance < 0:
        raise ValueError("Inductive reactance cannot be negative")
    if inductance == 0:
        return {"inductance": reactance / (2 * pi * frequency)}
    elif frequency == 0:
        return {"frequency": reactance / (2 * pi * inductance)}
    elif reactance == 0:
        return {"reactance": 2 * pi * frequency * inductance}
    else:
        raise ValueError("Exactly one argument must be 0")


if __name__ == "__main__":
    import doctest

View on GitHub (pinned to f5988cc097)

Solutions

  1. Set exactly one argument to 0 to mark the unknown: ind_reactance(35e-6, 1e3, 0) -> {'reactance': 0.2199114857512855}.
  2. Pre-check (inductance, frequency, reactance).count(0) == 1 before calling when inputs are dynamic.
  3. Do not call the function when you already have all three values — it is a solver, not a verifier.

Example fix

# before
ind_reactance(35e-3, 1e3, 50)  # ValueError: One and only one argument must be 0

# after
x_l = ind_reactance(35e-3, 1e3, 0)['reactance']  # solve for reactance
Defensive patterns

Strategy: validation

Validate before calling

if (inductance, frequency, reactance).count(0) != 1:
    raise ValueError('exactly one of L/f/XL must be 0 (the unknown)')

Type guard

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

Try / catch

try:
    out = ind_reactance(l, f, x)
except ValueError as exc:
    if 'must be 0' in str(exc):
        # fix the sentinel pattern, do not retry blindly
        ...
    raise

Prevention

When it happens

Trigger: ind_reactance(35e-3, 1e3, 50) (all known, nothing to solve); ind_reactance(0, 0, 50) (two zeros, underdetermined); passing None or -1 instead of 0 as the unknown marker.

Common situations: Passing a full triple of measured values from an instrument dump; forgetting the sentinel convention when migrating from an API that used None; looping over rows where some rows have all fields populated.

Related errors


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