TheAlgorithms/Python · error · ValueError
n must be >= 0
Error message
n must be >= 0
What it means
Raised by compute_nums() in project_euler/problem_046/sol1.py when n <= 0. The check 'if n <= 0' rejects both zero and negatives, but the message says 'n must be >= 0', so the message is misleading: passing 0 also raises even though 0 satisfies the stated constraint. Effectively the function requires n >= 1.
Source
Thrown at project_euler/problem_046/sol1.py:92
[5777, 5993]
>>> compute_nums(0)
Traceback (most recent call last):
...
ValueError: n must be >= 0
>>> compute_nums("a")
Traceback (most recent call last):
...
ValueError: n must be an integer
>>> compute_nums(1.1)
Traceback (most recent call last):
...
ValueError: n must be an integer
"""
if not isinstance(n, int):
raise ValueError("n must be an integer")
if n <= 0:
raise ValueError("n must be >= 0")
list_nums = []
for num in range(len(odd_composites)):
i = 0
while 2 * i * i <= odd_composites[num]:
rem = odd_composites[num] - 2 * i * i
if is_prime(rem):
break
i += 1
else:
list_nums.append(odd_composites[num])
if len(list_nums) == n:
return list_nums
return []
def solution() -> int:
View on GitHub (pinned to f5988cc097)
Solutions
- Pass n >= 1, since 0 is actually rejected by the guard.
- Short-circuit zero counts before calling: if n == 0: return [] (or equivalent) instead of calling compute_nums.
- If you must handle 0, wrap the call: result = [] if n == 0 else compute_nums(n).
- Upstream, treat the message as a doc bug and code against the real contract n >= 1.
Example fix
# before n = len(matches) # can be 0 nums = compute_nums(n) # ValueError: n must be >= 0 # after nums = compute_nums(n) if n > 0 else []
Defensive patterns
Strategy: validation
Validate before calling
if n < 1:
if n == 0:
result = [] # zero-count short circuit
else:
raise ValueError(f"n must be >= 1, got {n}")
else:
result = compute_nums(n) Type guard
def is_valid_count(n) -> bool:
return isinstance(n, int) and n >= 1 # real contract, despite message Try / catch
try:
nums = compute_nums(n)
except ValueError as e:
if str(e) == "n must be >= 0":
nums = [] # treat zero/negative request as empty result
else:
raise Prevention
- Code against the real contract (n >= 1); the error message is misleading about 0.
- Short-circuit zero counts before calling instead of relying on the exception.
- Watch for max(0, x) or empty-collection len() defaults feeding this parameter.
When it happens
Trigger: compute_nums(0) (raises despite the message implying 0 is allowed), compute_nums(-3). Any caller that clamps or defaults a count to 0 (e.g. max(0, len(results))) will trip this.
Common situations: Empty-input handling where a caller legitimately computes a count of 0 (empty list) and forwards it; default parameter values of 0; iterating ranges that start at 0.
Related errors
- Parameter nth must be greater than or equal to one.
- Hand should contain only 5 cards: {hand!r}
- gon_side must be in the range [3, 5]
- a gon ring should have a length that is a multiple of 3
- Please enter an integer greater than 0
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/f23503a164a27d0d.
Report an issue: GitHub.