{"record":{"id":"2534c1752de2c40c","repo":"TheAlgorithms/Python","slug":"parameter-nth-must-be-int-or-castable-to-int","errorCode":null,"errorMessage":"Parameter nth must be int or castable to int.","messagePattern":"Parameter nth must be int or castable to int\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"critical","filePath":"project_euler/problem_007/sol2.py","lineNumber":91,"sourceCode":"    ValueError: Parameter nth must be greater than or equal to one.\n    >>> solution(-17)\n    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":73,"sourceCodeEnd":107,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/project_euler/problem_007/sol2.py#L73-L107","documentation":"Documented TypeError from project_euler/problem_007/sol2.py:solution when int(nth) fails (solution('asd')). The raise even uses 'from None' (correct py3 style) but the except clause itself is py2 syntax ('except TypeError, ValueError:'), so under Python 3 the module fails to import and this TypeError is unreachable until the clause is fixed.","triggerScenarios":"After the fix: solution('asd'), solution(None). Today: importing or executing project_euler/problem_007/sol2.py under Python 3 raises SyntaxError('multiple exception types must be parenthesized') at line 91 instead.","commonSituations":"py3 test sweeps over the repo's euler solutions; unconverted CLI or query-string parameters reaching solution(); mixed py2/py3 idioms in one file (this file also uses modern 'list[int]' annotations, which makes the py2 except clause easy to miss).","solutions":["Change 'except TypeError, ValueError:' to 'except (TypeError, ValueError):' at project_euler/problem_007/sol2.py:91.","Then pass int or int-castable nth (e.g. 10001).","Validate nth with isinstance or isdigit checks in callers.","Run ruff/flake8 or py_compile over the module to catch the syntax class of error automatically."],"exampleFix":"# before\nexcept TypeError, ValueError:\n    raise TypeError('Parameter nth must be int or castable to int.') from None\n\n# after\nexcept (TypeError, ValueError):\n    raise TypeError('Parameter nth must be int or castable to int.') from None","handlingStrategy":"type-guard","validationCode":"nth = int(nth)\nif nth <= 0:\n    raise ValueError('nth must be >= 1')\nsolution(nth)","typeGuard":"def is_valid_prime_index(nth) -> bool:\n    return isinstance(nth, int) and not isinstance(nth, bool) and nth >= 1","tryCatchPattern":"try:\n    solution(nth)\nexcept TypeError as e:\n    if 'castable' in str(e):\n        ...","preventionTips":["Fix 'except TypeError, ValueError:' at problem_007/sol2.py:91 to parenthesized form - the file mixes py3 annotations with a py2 except, so the SyntaxError surprises people.","Validate nth (a positive index, not a prime value) before calling.","Use ruff/py_compile in CI to catch mixed py2 syntax."],"tags":["python-euler","python2-syntax","typeerror","validation","primes"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}