TheAlgorithms/Python · warning · ValueError

Final orbit radius must be greater than zero.

Error message

Final orbit radius must be greater than zero.

What it means

Raised in the __main__ interactive block of physics/orbital_transfer_work.py when the final orbit radius entered at the prompt is <= 0. As with the r1 check, the surrounding CLI re-reads r2 at line 67 after validation, so the checked value can be replaced by an unvalidated one before the calculation. The exception is caught by the local except ValueError and printed as 'Input error: ...'.

Source

Thrown at physics/orbital_transfer_work.py:62

    return f"{work:.3e}"


if __name__ == "__main__":
    import doctest

    doctest.testmod()
    print("Orbital transfer work calculator\n")

    try:
        M = float(input("Enter mass of central body (kg): ").strip())
        if M <= 0:
            r1 = float(input("Enter initial orbit radius (m): ").strip())
        if r1 <= 0:
            raise ValueError("Initial orbit radius must be greater than zero.")

        r2 = float(input("Enter final orbit radius (m): ").strip())
        if r2 <= 0:
            raise ValueError("Final orbit radius must be greater than zero.")
        m = float(input("Enter mass of orbiting object (kg): ").strip())
        if m <= 0:
            raise ValueError("Mass of the orbiting object must be greater than zero.")
        r1 = float(input("Enter initial orbit radius (m): ").strip())
        r2 = float(input("Enter final orbit radius (m): ").strip())

        result = orbital_transfer_work(M, m, r1, r2)
        print(f"Work done in orbital transfer: {result} Joules")

    except ValueError as e:
        print(f"Input error: {e}")

View on GitHub (pinned to f5988cc097)

Solutions

  1. Enter a strictly positive final radius in meters, e.g. 7000000
  2. Compute the radius (altitude + central body radius) before typing it in, rather than entering the altitude
  3. Skip the CLI: import orbital_transfer_work() and validate r_final > 0 in your own code
  4. If maintaining the script, delete the duplicate r1/r2 input calls at lines 66-67
Defensive patterns

Strategy: validation

Validate before calling

r2 = float(input("Enter final orbit radius (m): ").strip())
if r2 <= 0:
    raise ValueError(f"Final radius must be > 0, got {r2}")

Try / catch

try:
    result = orbital_transfer_work(M, m, r1, r2)
except ValueError as e:
    print(f"Input error: {e}")

Prevention

When it happens

Trigger: Running the script directly and answering the 'Enter final orbit radius (m):' prompt (either at line 60 or the duplicate at line 67) with 0 or a negative number such as -7e6.

Common situations: Entering a descent target expressed as a negative delta; mixing up meters and kilometers so a value like 7000 (which is positive but below the central body surface) passes while 0 does not; piped stdin automation feeding empty lines that float() converts to a ValueError caught as 'Input error'.

Related errors


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