{"record":{"id":"8fe7e94277ac22f9","repo":"TheAlgorithms/Python","slug":"parameter-n-must-be-int-or-castable-to-int-8fe7e9","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/sol1.py","lineNumber":84,"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    max_number = 0\n    if is_prime(n):\n        return n\n    while n % 2 == 0:\n        n //= 2\n    if is_prime(n):\n        return n\n    for i in range(3, int(math.sqrt(n)) + 1, 2):\n        if n % i == 0:\n            if is_prime(n // i):\n                max_number = n // i\n                break\n            elif is_prime(i):\n                max_number = i\n    return max_number\n","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/project_euler/problem_003/sol1.py#L66-L102","documentation":"Documented TypeError from project_euler/problem_003/sol1.py:solution when int(n) fails (e.g. solution('asd')). CAVEAT: the guard uses Python 2 syntax 'except TypeError, ValueError:' - under Python 3 this file raises SyntaxError at import, so the TypeError never actually fires until the clause is parenthesized.","triggerScenarios":"After the syntax fix: solution('asd'), solution(None), solution([1]). As written today: any import or execution of project_euler/problem_003/sol1.py under Python 3 fails with SyntaxError('multiple exception types must be parenthesized').","commonSituations":"Running this repo's doctests or test suite under Python 3; CI pipelines that import every solution module; migrating an old checkout of the algorithms repository to modern Python.","solutions":["Fix the except clause to 'except (TypeError, ValueError):' at project_euler/problem_003/sol1.py:84.","Then pass int or int-castable values (e.g. 600851475143, '13195').","Pre-check with isinstance(n, int) in caller code.","Include this module in doctest CI to catch the SyntaxError class of breakage."],"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 isinstance(n, (int, float)) and not str(n).isdigit():\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":["Repair the py2 except clause at problem_003/sol1.py:84 before anything else.","Pass ints or numeric strings only (floats like 2.5 will truncate silently).","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"}