TheAlgorithms/Python · error · ValueError
Parameter nth must be greater than or equal to one.
Error message
Parameter nth must be greater than or equal to one.
What it means
Raised by solution() in project_euler/problem_007/sol2.py when the requested prime index is not a positive integer. The function first coerces nth via int(nth) (raising TypeError for non-castable input), then enforces nth >= 1 because prime indexing is 1-based (prime #1 is 2). This ValueError guards the while-loop that collects primes from ever running with an impossible target.
Source
Thrown at project_euler/problem_007/sol2.py:93
Traceback (most recent call last):
...
ValueError: Parameter nth must be greater than or equal to one.
>>> solution([])
Traceback (most recent call last):
...
TypeError: Parameter nth must be int or castable to int.
>>> solution("asd")
Traceback (most recent call last):
...
TypeError: Parameter nth must be int or castable to int.
"""
try:
nth = int(nth)
except TypeError, ValueError:
raise TypeError("Parameter nth must be int or castable to int.") from None
if nth <= 0:
raise ValueError("Parameter nth must be greater than or equal to one.")
primes: list[int] = []
num = 2
while len(primes) < nth:
if is_prime(num):
primes.append(num)
num += 1
else:
num += 1
return primes[len(primes) - 1]
if __name__ == "__main__":
print(f"{solution() = }")
View on GitHub (pinned to f5988cc097)
Solutions
- Pass a 1-based positive integer: solution(1) returns the first prime (2).
- If your index is 0-based, convert before calling: solution(idx + 1).
- Validate user input before the call: if not isinstance(nth, int) or nth < 1: reject.
- Wrap the call in try/except (ValueError, TypeError) if nth comes from untrusted input.
Example fix
// before primes_wanted = start_index # 0-based from caller nth_prime = solution(primes_wanted) # ValueError when start_index == 0 // after primes_wanted = start_index + 1 # convert 0-based to 1-based nth_prime = solution(primes_wanted)
Defensive patterns
Strategy: validation
Validate before calling
def validate_nth(nth) -> int:
nth = int(nth) # may raise TypeError for bad input; let it propagate
if nth < 1:
raise ValueError(f"nth must be >= 1, got {nth}")
return nth
nth_prime = solution(validate_nth(user_nth)) Type guard
def is_valid_nth(nth) -> bool:
try:
return int(nth) >= 1
except (TypeError, ValueError):
return False Try / catch
try:
p = solution(nth)
except TypeError:
logger.error("nth is not int-castable: %r", nth)
except ValueError as e:
logger.error("nth out of range: %s", e) Prevention
- Treat the API as 1-based; convert 0-based indices with +1 before calling.
- Validate counts are >= 1 before forwarding user input.
- Remember strings like '10' are accepted (int() coercion), so reject non-numeric strings yourself if that matters.
When it happens
Trigger: Calling solution(0), solution(-5), or any call where int(nth) evaluates to <= 0 (e.g. solution("0"), solution(-3.7) which truncates to -3). Note solution(True) passes (int(True)==1) and solution("10") passes because strings are castable.
Common situations: Off-by-one bugs where a caller computes nth from a 0-based index (e.g. solution(idx) where idx can be 0); passing user input that was parsed as 0 or negative; test harnesses iterating ranges that include 0.
Related errors
- Please enter an integer greater than 0
- Parameters chain_length and number_limit must be greater tha
- Invalid input
- surface_area_cube() only accepts non-negative values
- surface_area_cuboid() only accepts non-negative values
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/5c41f93cc3327290.
Report an issue: GitHub.