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 couloumbs_law() in electronics/coulombs_law.py when the number of zeros among (force, charge1, charge2, distance) is not exactly 1. The function solves Coulomb's law F = k*q1*q2/r^2 for whichever argument is passed as 0 ('unknown'); zero for none or more than one argument leaves the system under- or over-determined.

Source

Thrown at electronics/coulombs_law.py:64

    >>> couloumbs_law(force=10, charge1=0, charge2=5, distance=2000)
    {'charge1': 0.0008900756564307966}

    >>> couloumbs_law(force=0, charge1=0, charge2=5, distance=2000)
    Traceback (most recent call last):
      ...
    ValueError: One and only one argument must be 0

    >>> couloumbs_law(force=0, charge1=3, charge2=5, distance=-2000)
    Traceback (most recent call last):
      ...
    ValueError: Distance cannot be negative

    """

    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__":

View on GitHub (pinned to f5988cc097)

Solutions

  1. Pass exactly one argument as 0 — the quantity to compute — and non-zero values for the other three.
  2. Note the function name is misspelled 'couloumbs_law'; make sure you are calling that exact symbol.
  3. Use keyword arguments to make the 'unknown' explicit and self-documenting.

Example fix

# before
couloumbs_law(force=10, charge1=3, charge2=5, distance=2000)  # no unknown

# after
force = couloumbs_law(force=0, charge1=3, charge2=5, distance=2000)['force']
Defensive patterns

Strategy: validation

Validate before calling

args = (force, charge1, charge2, distance)
if args.count(0) != 1:
    raise ValueError("pass exactly one 0 as the unknown")
result = couloumbs_law(*args)

Prevention

When it happens

Trigger: couloumbs_law(force=0, charge1=0, charge2=5, distance=2000) (two zeros), or a call where all four arguments are non-zero. 0 means 'solve for me', not a physical value.

Common situations: Treating 0 as a physical zero force/charge; passing all measured values and expecting a consistency check; argument mix-ups when calling positionally.

Related errors


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