TheAlgorithms/Python · warning · ValueError
Initial orbit radius must be greater than zero.
Error message
Initial orbit radius must be greater than zero.
What it means
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: ...'.
Source
Thrown at physics/orbital_transfer_work.py:58
work = (gravitational_constant * mass_central * mass_object / 2) * (
1 / r_initial - 1 / r_final
)
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
- 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
Example fix
# before (module __main__ block)
M = float(input(...))
if M <= 0:
r1 = float(input(...))
if r1 <= 0:
raise ValueError("Initial orbit radius must be greater than zero.")
...
r1 = float(input(...)) # re-read overwrites the validated value
# after
M = float(input("Enter mass of central body (kg): ").strip())
r1 = float(input("Enter initial orbit radius (m): ").strip())
if r1 <= 0:
raise ValueError("Initial orbit radius must be greater than zero.") Defensive patterns
Strategy: validation
Validate before calling
def read_positive(prompt):
while True:
v = float(input(prompt).strip())
if v > 0:
return v
print("Value must be greater than zero.")
r1 = read_positive("Enter initial orbit radius (m): ") Try / catch
try:
...
except ValueError as e:
print(f"Input error: {e}") # the script already catches and prints; keep prompts positive Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Final orbit radius must be greater than zero.
- Mass of the orbiting object must be greater than zero.
- Orbital radii must be greater than zero.
- Expected a_coeffs to have {self.order + 1} elements for {sel
- n must not be negative
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/49d104351fbb0b2d.
Report an issue: GitHub.