{"record":{"id":"17ffc74b5680d1b6","repo":"TheAlgorithms/Python","slug":"orbital-radii-must-be-greater-than-zero","errorCode":null,"errorMessage":"Orbital radii must be greater than zero.","messagePattern":"Orbital radii must be greater than zero\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"physics/orbital_transfer_work.py","lineNumber":39,"sourceCode":"        mass_object (float): Mass of the object being moved (kg)\n        r_initial (float): Initial orbital radius (m)\n        r_final (float): Final orbital radius (m)\n\n    Returns:\n        str: Work done in Joules as a string in scientific notation (3 decimals)\n\n    Examples:\n        >>> orbital_transfer_work(5.972e24, 1000, 6.371e6, 7e6)\n        '2.811e+09'\n        >>> orbital_transfer_work(5.972e24, 500, 7e6, 6.371e6)\n        '-1.405e+09'\n        >>> orbital_transfer_work(1.989e30, 1000, 1.5e11, 2.28e11)\n        '1.514e+11'\n    \"\"\"\n    gravitational_constant = 6.67430e-11\n\n    if r_initial <= 0 or r_final <= 0:\n        raise ValueError(\"Orbital radii must be greater than zero.\")\n\n    work = (gravitational_constant * mass_central * mass_object / 2) * (\n        1 / r_initial - 1 / r_final\n    )\n    return f\"{work:.3e}\"\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n    print(\"Orbital transfer work calculator\\n\")\n\n    try:\n        M = float(input(\"Enter mass of central body (kg): \").strip())\n        if M <= 0:\n            r1 = float(input(\"Enter initial orbit radius (m): \").strip())\n        if r1 <= 0:","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/physics/orbital_transfer_work.py#L21-L57","documentation":"Raised by orbital_transfer_work(mass_central, mass_object, r_initial, r_final) when r_initial <= 0 or r_final <= 0. Orbital radii are distances from the central body and must be strictly positive; zero or negative radii make the 1/r terms in the work formula singular or unphysical, so they are rejected before computing W = G*M*m/2 * (1/r_initial - 1/r_final).","triggerScenarios":"orbital_transfer_work(5.972e24, 1000, 0, 7e6); orbital_transfer_work(5.972e24, 1000, -6.371e6, 7e6); radii derived from altitude minus Earth radius that came out <= 0 because the altitude was below ground or zero.","commonSituations":"Passing altitude (height above surface) instead of radius (altitude + body radius), which yields tiny or negative values; swapping argument order so a mass lands in an r slot; simulation edge cases where an orbit decays to r=0.","solutions":["Pass orbital radius, not altitude: r = altitude + central_body_radius (e.g. 6.371e6 for Earth)","Check the argument order matches (mass_central, mass_object, r_initial, r_final)","Clamp or reject simulation states with r <= 0 before calling the function"],"exampleFix":"# before\norbital_transfer_work(5.972e24, 1000, 400e3, 7e6)  # altitude, not radius\n# ValueError: Orbital radii must be greater than zero.\n\n# after\norbital_transfer_work(5.972e24, 1000, 400e3 + 6.371e6, 7e6)","handlingStrategy":"validation","validationCode":"def radius_from_altitude(altitude, body_radius):\n    r = altitude + body_radius\n    if r <= 0:\n        raise ValueError(f\"radius {r} must be > 0 (altitude={altitude})\")\n    return r\n\norbital_transfer_work(5.972e24, 1000, radius_from_altitude(alt1, 6.371e6), radius_from_altitude(alt2, 6.371e6))","typeGuard":null,"tryCatchPattern":"try:\n    w = orbital_transfer_work(M, m, r1, r2)\nexcept ValueError as e:\n    if \"Orbital radii\" in str(e):\n        raise ValueError(f\"bad radii: r1={r1}, r2={r2}\") from e\n    raise","preventionTips":["Always pass radius (altitude + body radius), never altitude","Check r_initial > 0 and r_final > 0 before the call since the message does not say which failed"],"tags":["physics","orbital-mechanics","validation","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}