TheAlgorithms/Python · error · ValueError
n must not be negative
Error message
n must not be negative
What it means
Raised by capture_area() in physics/basic_orbital_capture.py when the capture radius passed in is negative. The function computes sigma = pi * r^2, the effective cross-sectional capture area; a negative radius has no physical meaning. The guard sits between the docstring and the pi*r**2 computation.
Source
Thrown at backtracking/all_combinations.py:57
>>> generate_all_combinations(n=5, k=4)
[[1, 2, 3, 4], [1, 2, 3, 5], [1, 2, 4, 5], [1, 3, 4, 5], [2, 3, 4, 5]]
>>> generate_all_combinations(n=3, k=3)
[[1, 2, 3]]
>>> generate_all_combinations(n=3, k=1)
[[1], [2], [3]]
>>> generate_all_combinations(n=1, k=0)
[[]]
>>> generate_all_combinations(n=1, k=1)
[[1]]
>>> from itertools import combinations
>>> all(generate_all_combinations(n, k) == combination_lists(n, k)
... for n in range(1, 6) for k in range(1, 6))
True
"""
if k < 0:
raise ValueError("k must not be negative")
if n < 0:
raise ValueError("n must not be negative")
result: list[list[int]] = []
create_all_state(1, n, k, [], result)
return result
def create_all_state(
increment: int,
total_number: int,
level: int,
current_list: list[int],
total_list: list[list[int]],
) -> None:
"""
Helper function to recursively build all combinations.
>>> create_all_state(1, 4, 2, [], result := [])
>>> resultView on GitHub (pinned to f5988cc097)
Solutions
- Validate that the radius is >= 0 before calling capture_area().
- If the radius comes from capture_radii(), inspect why it went negative — that function itself already guards mass/radius, so the sign flip happened in your code.
- Use abs() only if the negative sign is provably a sign-convention artifact, not bad data.
Example fix
# before
area = capture_area(some_radius)
# after
if some_radius < 0:
raise ValueError(f"radius must be >= 0, got {some_radius}")
area = capture_area(some_radius) Defensive patterns
Strategy: validation
Validate before calling
if capture_radius < 0:
raise ValueError(f"capture_radius must be >= 0, got {capture_radius}")
sigma = capture_area(capture_radius) Type guard
def is_non_negative(x: object) -> bool:
return isinstance(x, (int, float)) and not isinstance(x, bool) and x >= 0 Try / catch
try:
sigma = capture_area(r)
except ValueError:
sigma = capture_area(abs(r)) # only when sign is a proven artifact Prevention
- Chain capture_radii() output directly; it never returns negative radii.
- Validate intermediate radius arithmetic for sign flips.
- Unit-test the boundary r=0 (returns 0.0, does not raise).
When it happens
Trigger: Calling capture_area(-1); passing the unvalidated result of another computation that produced a negative radius; chaining capture_radii output through a subtraction that flipped sign.
Common situations: Reusing a radius variable that was negated elsewhere, or passing an error code / sentinel negative number straight into the function without checking.
Related errors
- Expected a_coeffs to have {self.order + 1} elements for {sel
- Depth cannot be less than 0
- Invalid velocity. Should be a positive number.
- All input parameters must be positive
- Invalid inputs. Enter positive value.
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/11d659bf201d2468.
Report an issue: GitHub.