TheAlgorithms/Python · error · ValueError
All elements in candidates must be non-negative
Error message
All elements in candidates must be non-negative
What it means
Raised by casimir_force() in physics/casimir_effect.py when the force argument is negative (and exactly one argument was 0). The Casimir force magnitude in this formula is treated as a non-negative quantity; a negative magnitude is rejected. Note force is checked first, so a negative force raises even if area or distance is also negative.
Source
Thrown at backtracking/combination_sum.py:60
"""
>>> combination_sum([2, 3, 5], 8)
[[2, 2, 2, 2], [2, 3, 3], [3, 5]]
>>> combination_sum([2, 3, 6, 7], 7)
[[2, 2, 3], [7]]
>>> combination_sum([-8, 2.3, 0], 1)
Traceback (most recent call last):
...
ValueError: All elements in candidates must be non-negative
>>> combination_sum([], 1)
Traceback (most recent call last):
...
ValueError: Candidates list should not be empty
"""
if not candidates:
raise ValueError("Candidates list should not be empty")
if any(x < 0 for x in candidates):
raise ValueError("All elements in candidates must be non-negative")
path = [] # type: list[int]
answer = [] # type: list[int]
backtrack(candidates, path, answer, target, 0)
return answer
def main() -> None:
print(combination_sum([-8, 2.3, 0], 1))
if __name__ == "__main__":
import doctest
doctest.testmod()
main()
View on GitHub (pinned to f5988cc097)
Solutions
- Pass the magnitude: use abs(force) if the sign only encodes attraction direction.
- Check force >= 0 before the call if the value is user-supplied.
- Wrap in try/except ValueError to surface a clear message for bad imports.
Example fix
# before casimir_force(force=measured_force, area=0, distance=d) # -912e-12 # after casimir_force(force=abs(measured_force), area=0, distance=d)
Defensive patterns
Strategy: validation
Validate before calling
if force is not None and force < 0:
force = abs(force) # magnitude API; sign encodes attraction elsewhere
casimir_force(force=force, area=a, distance=d) Try / catch
try:
casimir_force(force=f, area=0, distance=d)
except ValueError as e:
if "force" in str(e):
casimir_force(force=abs(f), area=0, distance=d)
else:
raise Prevention
- Convert signed measurements to magnitudes at ingest.
- Note force is checked before distance/area, so it masks co-occurring negative inputs.
- Keep attraction-direction metadata in a separate column, not the sign of force.
When it happens
Trigger: casimir_force(force=-912e-12, area=0, distance=0.09374); passing a signed force measurement where the sign convention was 'attractive = negative' into a magnitude-based API.
Common situations: Physics datasets often encode the attractive Casimir force as negative; feeding such measurements directly into this magnitude-based solver without taking abs() triggers the error.
Related errors
- Open Knight Tour cannot be performed on a board of size {n}
- Expected a_coeffs to have {self.order + 1} elements for {sel
- Expected b_coeffs to have {self.order + 1} elements for {sel
- k must not be negative
- n must not be negative
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/6705ca18cdc67724.
Report an issue: GitHub.