TheAlgorithms/Python · warning · ValueError

Mass of the orbiting object must be greater than zero.

Error message

Mass of the orbiting object must be greater than zero.

What it means

Raised in the __main__ interactive block of physics/orbital_transfer_work.py when the orbiting object's mass entered at the prompt is <= 0. The function itself never checks mass_object, so this error exists only in the interactive path; it is caught locally and printed as 'Input error: ...'. The central mass M is never validated at all — the 'if M <= 0' branch misreads r1 instead of raising.

Source

Thrown at physics/orbital_transfer_work.py:65

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 mass such as 1000 (kg)
  2. If a zero mass is genuinely intended (test particle), call orbital_transfer_work() directly — the library function does not raise this error
  3. When maintaining the script, add a proper 'if M <= 0' check for the central mass, which is currently missing
Defensive patterns

Strategy: validation

Validate before calling

m = float(input("Enter mass of orbiting object (kg): ").strip())
if m <= 0:
    raise ValueError(f"Mass must be > 0, got {m}")

# library callers: this check exists only in the CLI, not in orbital_transfer_work()

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 module directly and answering 'Enter mass of orbiting object (kg):' with 0 or a negative number, e.g. -1000 or 0.

Common situations: Entering mass in wrong units (0-heavy values like 0 for a satellite); testing the CLI with placeholder zeros; forgetting that the library function accepts any mass sign and only the CLI enforces positivity.

Related errors


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