{"record":{"id":"49d104351fbb0b2d","repo":"TheAlgorithms/Python","slug":"initial-orbit-radius-must-be-greater-than-zero","errorCode":null,"errorMessage":"Initial orbit radius must be greater than zero.","messagePattern":"Initial orbit radius must be greater than zero\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"warning","filePath":"physics/orbital_transfer_work.py","lineNumber":58,"sourceCode":"\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:\n            raise ValueError(\"Initial orbit radius must be greater than zero.\")\n\n        r2 = float(input(\"Enter final orbit radius (m): \").strip())\n        if r2 <= 0:\n            raise ValueError(\"Final orbit radius must be greater than zero.\")\n        m = float(input(\"Enter mass of orbiting object (kg): \").strip())\n        if m <= 0:\n            raise ValueError(\"Mass of the orbiting object must be greater than zero.\")\n        r1 = float(input(\"Enter initial orbit radius (m): \").strip())\n        r2 = float(input(\"Enter final orbit radius (m): \").strip())\n\n        result = orbital_transfer_work(M, m, r1, r2)\n        print(f\"Work done in orbital transfer: {result} Joules\")\n\n    except ValueError as e:\n        print(f\"Input error: {e}\")\n","sourceCodeStart":40,"sourceCodeEnd":74,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/physics/orbital_transfer_work.py#L40-L74","documentation":"Raised in the __main__ interactive block of physics/orbital_transfer_work.py when the initial orbit radius entered at the prompt is <= 0. Note the surrounding CLI code is buggy: r1 is prompted inside 'if M <= 0:' and prompted a second time later (lines 56 and 66), so the validated value is overwritten by an unvalidated re-read. The exception is caught locally and printed as 'Input error: ...'.","triggerScenarios":"Running the module directly (python physics/orbital_transfer_work.py), entering a central mass M > 0 (so the first r1 prompt is skipped and r1 is undefined until line 66), then entering 0 or a negative number for the second r1 prompt at line 66 — this raises before the print, but note that r1 from line 66 is checked by the line 57 test against the OLD r1.","commonSituations":"Users running the script interactively and entering an altitude or negative value; entering a positive M, which skips the first r1 read and makes the flow depend on the buggy re-read; CI invoking the script with piped stdin containing invalid radii.","solutions":["Enter a strictly positive initial radius such as 7000000 (meters) when prompted","Prefer importing the function (from physics.orbital_transfer_work import orbital_transfer_work) and validating inputs yourself rather than using the buggy CLI","If you maintain this file, remove the duplicated r1/r2 reads (lines 66-67) and the misplaced 'if M <= 0' so each value is read and checked exactly once"],"exampleFix":"# before (module __main__ block)\nM = float(input(...))\nif M <= 0:\n    r1 = float(input(...))\nif r1 <= 0:\n    raise ValueError(\"Initial orbit radius must be greater than zero.\")\n...\nr1 = float(input(...))  # re-read overwrites the validated value\n\n# after\nM = float(input(\"Enter mass of central body (kg): \").strip())\nr1 = float(input(\"Enter initial orbit radius (m): \").strip())\nif r1 <= 0:\n    raise ValueError(\"Initial orbit radius must be greater than zero.\")","handlingStrategy":"validation","validationCode":"def read_positive(prompt):\n    while True:\n        v = float(input(prompt).strip())\n        if v > 0:\n            return v\n        print(\"Value must be greater than zero.\")\n\nr1 = read_positive(\"Enter initial orbit radius (m): \")","typeGuard":null,"tryCatchPattern":"try:\n    ...\nexcept ValueError as e:\n    print(f\"Input error: {e}\")  # the script already catches and prints; keep prompts positive","preventionTips":["Enter strictly positive radii in meters at the prompt","Prefer the library function over the buggy CLI when embedding","If maintaining the script, remove the duplicated r1/r2 input lines"],"tags":["physics","cli","user-input","valueerror","orbital-mechanics"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}