TheAlgorithms/Python · warning · ValueError

Exactly one argument must be 0

Error message

Exactly one argument must be 0

What it means

Defensive trailing raise at the end of couloumbs_law(): if none of the four elif branches matched, the solver fell through. In practice this line is unreachable — the earlier count(0) != 1 check guarantees exactly one argument is 0 and the elif chain (force/charge1/charge2/distance == 0) covers all four cases — so seeing it indicates the guard logic was modified or bypassed.

Source

Thrown at electronics/coulombs_law.py:79

    charge_product = abs(charge1 * charge2)

    if (force, charge1, charge2, distance).count(0) != 1:
        raise ValueError("One and only one argument must be 0")
    if distance < 0:
        raise ValueError("Distance cannot be negative")
    if force == 0:
        force = COULOMBS_CONSTANT * charge_product / (distance**2)
        return {"force": force}
    elif charge1 == 0:
        charge1 = abs(force) * (distance**2) / (COULOMBS_CONSTANT * charge2)
        return {"charge1": charge1}
    elif charge2 == 0:
        charge2 = abs(force) * (distance**2) / (COULOMBS_CONSTANT * charge1)
        return {"charge2": charge2}
    elif distance == 0:
        distance = (COULOMBS_CONSTANT * charge_product / abs(force)) ** 0.5
        return {"distance": distance}
    raise ValueError("Exactly one argument must be 0")


if __name__ == "__main__":
    import doctest

    doctest.testmod()

View on GitHub (pinned to f5988cc097)

Solutions

  1. If you hit this, restore the original precondition: exactly one of (force, charge1, charge2, distance) must be 0.
  2. Do not copy the elif chain without the count(0) != 1 guard that precedes it.
  3. Treat it as an invariant assertion failure — audit any local modifications to coulombs_law.py.
Defensive patterns

Strategy: try-catch

Try / catch

try:
    result = couloumbs_law(force, charge1, charge2, distance)
except ValueError as e:
    if "Exactly one argument must be 0" in str(e):
        raise RuntimeError("invariant broken — coulombs_law.py was modified") from e
    raise

Prevention

When it happens

Trigger: Not reachable through the public API as shipped. Could fire if someone edits the function to remove or reorder the count(0) check or the elif branches.

Common situations: Forked/patched copies of the file where validation was loosened; copy-pasting the solver body into new code without copying the precondition check.

Related errors


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