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

Raised by gravitational_law() in physics/newtons_law_of_gravitation.py when the number of arguments equal to 0 among (force, mass_1, mass_2, distance) is not exactly one. The function is a solver: it computes whichever single quantity you pass as 0 from the other three, so exactly one placeholder zero is required. Zero, two, three, or four zeros all trigger this error before any physics is computed.

Source

Thrown at physics/newtons_law_of_gravitation.py:73

    Traceback (most recent call last):
        ...
    ValueError: One and only one argument must be 0

    >>> gravitational_law(force=36337.283, mass_1=-674, mass_2=0, distance=35584)
    Traceback (most recent call last):
        ...
    ValueError: Mass can not be negative

    >>> gravitational_law(force=-847938e12, mass_1=674, mass_2=0, distance=9374)
    Traceback (most recent call last):
        ...
    ValueError: Gravitational force can not be negative
    """

    product_of_mass = mass_1 * mass_2

    if (force, mass_1, mass_2, distance).count(0) != 1:
        raise ValueError("One and only one argument must be 0")
    if force < 0:
        raise ValueError("Gravitational force can not be negative")
    if distance < 0:
        raise ValueError("Distance can not be negative")
    if mass_1 < 0 or mass_2 < 0:
        raise ValueError("Mass can not be negative")
    if force == 0:
        force = GRAVITATIONAL_CONSTANT * product_of_mass / (distance**2)
        return {"force": force}
    elif mass_1 == 0:
        mass_1 = (force) * (distance**2) / (GRAVITATIONAL_CONSTANT * mass_2)
        return {"mass_1": mass_1}
    elif mass_2 == 0:
        mass_2 = (force) * (distance**2) / (GRAVITATIONAL_CONSTANT * mass_1)
        return {"mass_2": mass_2}
    elif distance == 0:
        distance = (GRAVITATIONAL_CONSTANT * product_of_mass / (force)) ** 0.5
        return {"distance": distance}

View on GitHub (pinned to f5988cc097)

Solutions

  1. Pass exactly one argument as 0 to designate the unknown, e.g. gravitational_law(force=0, mass_1=674, mass_2=5.9e24, distance=9374)
  2. If you have all four values and only want to verify them, add your own check instead of calling the solver
  3. In a UI, map empty input fields to 0 for the single unknown field and reject forms where more than one field is empty
  4. Wrap the call in try/except ValueError if the argument count depends on runtime user input

Example fix

// before
gravitational_law(force=0, mass_1=0, mass_2=5.9e24, distance=6.4e6)
# ValueError: One and only one argument must be 0

# after (solve for mass_1)
mass_1 = gravitational_law(force=0, mass_1=0, mass_2=5.9e24, distance=6.4e6)["mass_1"]
Defensive patterns

Strategy: validation

Validate before calling

def validate_grav_args(force, mass_1, mass_2, distance):
    n = sum(v == 0 for v in (force, mass_1, mass_2, distance))
    if n != 1:
        raise ValueError(f"Expected exactly one 0 placeholder, got {n}")

validate_grav_args(force, mass_1, mass_2, distance)
gravitational_law(force, mass_1, mass_2, distance)

Type guard

def has_single_zero(*vals) -> bool:
    return sum(v == 0 for v in vals) == 1

Try / catch

try:
    result = gravitational_law(f, m1, m2, d)
except ValueError as e:
    if "One and only one" in str(e):
        result = None  # ask user which single quantity to solve for
    else:
        raise

Prevention

When it happens

Trigger: gravitational_law(force=0, mass_1=0, mass_2=5, distance=10) (two zeros); gravitational_law(force=100, mass_1=5, mass_2=5, distance=10) (no zero, nothing to solve for); gravitational_law(force=0, mass_1=0, mass_2=0, distance=0).

Common situations: Porting code that used None or -1 as the 'unknown' sentinel instead of 0; building a UI where the user leaves several fields blank (mapped to 0); forgetting the solver convention and passing all four measured values expecting a consistency check.

Related errors


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