TheAlgorithms/Python · error · ValueError
Candidates list should not be empty
Error message
Candidates list should not be empty
What it means
Raised by casimir_force() in physics/casimir_effect.py when the number of zero-valued arguments among (force, area, distance) is not exactly one. The API is solve-for-one: you pass 0 for the quantity you want computed and real values for the other two. Two zeros (nothing to solve from), three zeros (all unknown), or zero zeros (nothing requested) all trigger this.
Source
Thrown at backtracking/combination_sum.py:57
def combination_sum(candidates: list, target: int) -> list:
"""
>>> 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()View on GitHub (pinned to f5988cc097)
Solutions
- Pass exactly one 0 for the quantity you want returned, e.g. casimir_force(force=0, area=0.002, distance=0.003) returns {'force': ...}.
- If some inputs are genuinely unknown, substitute measured/estimated values rather than 0.
- Add a wrapper that asserts sum(1 for v in (force, area, distance) if v == 0) == 1 before delegating.
Example fix
# before f = casimir_force(force=0, area=0, distance=0.003) # two zeros # after f = casimir_force(force=0, area=0.002, distance=0.003) # solve for force
Defensive patterns
Strategy: validation
Validate before calling
args = (force, area, distance)
if sum(1 for v in args if v == 0) != 1:
raise ValueError("pass exactly one 0 to casimir_force for the unknown quantity")
result = casimir_force(*args) Try / catch
try:
result = casimir_force(force=f, area=a, distance=d)
except ValueError as e:
if "must be 0" in str(e):
raise ValueError("casimir_force needs exactly one unknown (0); got " + repr((f, a, d))) from e
raise Prevention
- Use keyword arguments to make the solve-for-one intent explicit.
- Never default unset parameters to 0 in wrappers.
- Document the pass-zero convention at every call site.
When it happens
Trigger: casimir_force(force=3457e-12, area=0, distance=0) (two zeros); casimir_force(force=1e-9, area=0.002, distance=0.003) (no zeros — nothing to solve); casimir_force(0, 0, 0).
Common situations: Developers assume all three arguments are always supplied with real values (typical function contract) instead of the pass-zero-for-the-unknown convention; or code iterates over parameter sets and passes default 0.0 for unset fields, accidentally creating multiple zeros.
Related errors
- Expected a_coeffs to have {self.order + 1} elements for {sel
- n must not be negative
- Depth cannot be less than 0
- Invalid input needed_sum must be between 1 and 1000, power b
- Invalid velocity. Should be a positive number.
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/d246df27c5680672.
Report an issue: GitHub.