{"record":{"id":"5c41f93cc3327290","repo":"TheAlgorithms/Python","slug":"parameter-nth-must-be-greater-than-or-equal-to-one","errorCode":null,"errorMessage":"Parameter nth must be greater than or equal to one.","messagePattern":"Parameter nth must be greater than or equal to one\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"project_euler/problem_007/sol2.py","lineNumber":93,"sourceCode":"    Traceback (most recent call last):\n        ...\n    ValueError: Parameter nth must be greater than or equal to one.\n    >>> solution([])\n    Traceback (most recent call last):\n        ...\n    TypeError: Parameter nth must be int or castable to int.\n    >>> solution(\"asd\")\n    Traceback (most recent call last):\n        ...\n    TypeError: Parameter nth must be int or castable to int.\n    \"\"\"\n\n    try:\n        nth = int(nth)\n    except TypeError, ValueError:\n        raise TypeError(\"Parameter nth must be int or castable to int.\") from None\n    if nth <= 0:\n        raise ValueError(\"Parameter nth must be greater than or equal to one.\")\n    primes: list[int] = []\n    num = 2\n    while len(primes) < nth:\n        if is_prime(num):\n            primes.append(num)\n            num += 1\n        else:\n            num += 1\n    return primes[len(primes) - 1]\n\n\nif __name__ == \"__main__\":\n    print(f\"{solution() = }\")\n","sourceCodeStart":75,"sourceCodeEnd":107,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/project_euler/problem_007/sol2.py#L75-L107","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nprimes_wanted = start_index  # 0-based from caller\nnth_prime = solution(primes_wanted)  # ValueError when start_index == 0\n\n// after\nprimes_wanted = start_index + 1  # convert 0-based to 1-based\nnth_prime = solution(primes_wanted)","handlingStrategy":"validation","validationCode":"def validate_nth(nth) -> int:\n    nth = int(nth)  # may raise TypeError for bad input; let it propagate\n    if nth < 1:\n        raise ValueError(f\"nth must be >= 1, got {nth}\")\n    return nth\n\nnth_prime = solution(validate_nth(user_nth))","typeGuard":"def is_valid_nth(nth) -> bool:\n    try:\n        return int(nth) >= 1\n    except (TypeError, ValueError):\n        return False","tryCatchPattern":"try:\n    p = solution(nth)\nexcept TypeError:\n    logger.error(\"nth is not int-castable: %r\", nth)\nexcept ValueError as e:\n    logger.error(\"nth out of range: %s\", e)","preventionTips":["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."],"tags":["project-euler","validation","valueerror","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}