{"record":{"id":"e81f5115a71431e2","repo":"TheAlgorithms/Python","slug":"parameter-n-must-be-int-or-castable-to-int-e81f51","errorCode":null,"errorMessage":"Parameter n must be int or castable to int.","messagePattern":"Parameter n must be int or castable to int\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"critical","filePath":"project_euler/problem_003/sol3.py","lineNumber":48,"sourceCode":"    ValueError: Parameter n must be greater than or equal to one.\n    >>> solution(-17)\n    Traceback (most recent call last):\n        ...\n    ValueError: Parameter n must be greater than or equal to one.\n    >>> solution([])\n    Traceback (most recent call last):\n        ...\n    TypeError: Parameter n must be int or castable to int.\n    >>> solution(\"asd\")\n    Traceback (most recent call last):\n        ...\n    TypeError: Parameter n must be int or castable to int.\n    \"\"\"\n\n    try:\n        n = int(n)\n    except TypeError, ValueError:\n        raise TypeError(\"Parameter n must be int or castable to int.\")\n    if n <= 0:\n        raise ValueError(\"Parameter n must be greater than or equal to one.\")\n    i = 2\n    ans = 0\n    if n == 2:\n        return 2\n    while n > 2:\n        while n % i != 0:\n            i += 1\n        ans = i\n        while n % i == 0:\n            n = n // i\n        i += 1\n    return int(ans)\n\n\nif __name__ == \"__main__\":\n    print(f\"{solution() = }\")","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/project_euler/problem_003/sol3.py#L30-L66","documentation":"Documented TypeError from project_euler/problem_003/sol3.py:solution when int(n) fails (solution('asd')). CAVEAT: the file's validation uses Python 2 syntax 'except TypeError, ValueError:' and therefore raises SyntaxError under Python 3 at import time - the documented TypeError cannot fire until the clause is rewritten as 'except (TypeError, ValueError):'.","triggerScenarios":"After the syntax fix: solution('asd'), solution(None), solution(3.5) is fine (casts), solution(object()) raises. Today: running or importing the module under Python 3 produces SyntaxError('multiple exception types must be parenthesized').","commonSituations":"Whole-repo doctest sweeps under Python 3; migration of an old algorithms checkout; harnesses that call every problem_003 solution variant with the same inputs, where sol1/sol2/sol3 all share the same broken guard.","solutions":["Parenthesize the exceptions at project_euler/problem_003/sol3.py:48.","Pass int or int-castable values afterwards.","Guard calls with a small is_valid_n helper shared across the problem_003 variants.","Add compile checks for legacy modules to CI."],"exampleFix":"# before\nexcept TypeError, ValueError:\n    raise TypeError('Parameter n must be int or castable to int.')\n\n# after\nexcept (TypeError, ValueError):\n    raise TypeError('Parameter n must be int or castable to int.') from None","handlingStrategy":"type-guard","validationCode":"if not is_int_castable(n):\n    raise TypeError(f'n must be int-castable, got {n!r}')\nsolution(n)","typeGuard":"def is_int_castable(v) -> bool:\n    try:\n        int(v)\n        return True\n    except (TypeError, ValueError):\n        return False","tryCatchPattern":"try:\n    solution(n)\nexcept TypeError:\n    ...","preventionTips":["Parenthesize the except clause at problem_003/sol3.py:48 first.","Share one input-validation helper across all problem_003 variants.","Compile-check legacy modules in CI."],"tags":["python-euler","python2-syntax","typeerror","validation","prime-factorization"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}