{"record":{"id":"6c466c99edce1f3b","repo":"TheAlgorithms/Python","slug":"one-and-only-one-argument-must-be-0-6c466c","errorCode":null,"errorMessage":"One and only one argument must be 0","messagePattern":"One and only one argument must be 0","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"physics/newtons_law_of_gravitation.py","lineNumber":73,"sourceCode":"    Traceback (most recent call last):\n        ...\n    ValueError: One and only one argument must be 0\n\n    >>> gravitational_law(force=36337.283, mass_1=-674, mass_2=0, distance=35584)\n    Traceback (most recent call last):\n        ...\n    ValueError: Mass can not be negative\n\n    >>> gravitational_law(force=-847938e12, mass_1=674, mass_2=0, distance=9374)\n    Traceback (most recent call last):\n        ...\n    ValueError: Gravitational force can not be negative\n    \"\"\"\n\n    product_of_mass = mass_1 * mass_2\n\n    if (force, mass_1, mass_2, distance).count(0) != 1:\n        raise ValueError(\"One and only one argument must be 0\")\n    if force < 0:\n        raise ValueError(\"Gravitational force can not be negative\")\n    if distance < 0:\n        raise ValueError(\"Distance can not be negative\")\n    if mass_1 < 0 or mass_2 < 0:\n        raise ValueError(\"Mass can not be negative\")\n    if force == 0:\n        force = GRAVITATIONAL_CONSTANT * product_of_mass / (distance**2)\n        return {\"force\": force}\n    elif mass_1 == 0:\n        mass_1 = (force) * (distance**2) / (GRAVITATIONAL_CONSTANT * mass_2)\n        return {\"mass_1\": mass_1}\n    elif mass_2 == 0:\n        mass_2 = (force) * (distance**2) / (GRAVITATIONAL_CONSTANT * mass_1)\n        return {\"mass_2\": mass_2}\n    elif distance == 0:\n        distance = (GRAVITATIONAL_CONSTANT * product_of_mass / (force)) ** 0.5\n        return {\"distance\": distance}","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/physics/newtons_law_of_gravitation.py#L55-L91","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["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)","If you have all four values and only want to verify them, add your own check instead of calling the solver","In a UI, map empty input fields to 0 for the single unknown field and reject forms where more than one field is empty","Wrap the call in try/except ValueError if the argument count depends on runtime user input"],"exampleFix":"// before\ngravitational_law(force=0, mass_1=0, mass_2=5.9e24, distance=6.4e6)\n# ValueError: One and only one argument must be 0\n\n# after (solve for mass_1)\nmass_1 = gravitational_law(force=0, mass_1=0, mass_2=5.9e24, distance=6.4e6)[\"mass_1\"]","handlingStrategy":"validation","validationCode":"def validate_grav_args(force, mass_1, mass_2, distance):\n    n = sum(v == 0 for v in (force, mass_1, mass_2, distance))\n    if n != 1:\n        raise ValueError(f\"Expected exactly one 0 placeholder, got {n}\")\n\nvalidate_grav_args(force, mass_1, mass_2, distance)\ngravitational_law(force, mass_1, mass_2, distance)","typeGuard":"def has_single_zero(*vals) -> bool:\n    return sum(v == 0 for v in vals) == 1","tryCatchPattern":"try:\n    result = gravitational_law(f, m1, m2, d)\nexcept ValueError as e:\n    if \"One and only one\" in str(e):\n        result = None  # ask user which single quantity to solve for\n    else:\n        raise","preventionTips":["Treat 0 as the solver sentinel: exactly one argument, all others strictly positive","Do not use None or -1 as the unknown marker with this API","In forms, allow at most one blank (blank -> 0) field"],"tags":["physics","argument-validation","solver-api","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}