{"record":{"id":"7a2f78d12ac8f2cc","repo":"TheAlgorithms/Python","slug":"parameter-n-must-be-int-or-castable-to-int-7a2f78","errorCode":null,"errorMessage":"Parameter n must be int or castable to int.","messagePattern":"Parameter n must be int or castable to int\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"critical","filePath":"project_euler/problem_005/sol1.py","lineNumber":51,"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 = 0\n    while 1:\n        i += n * (n - 1)\n        nfound = 0\n        for j in range(2, n):\n            if i % j != 0:\n                nfound = 1\n                break\n        if nfound == 0:\n            if i == 0:\n                i = 1\n            return i\n    return None\n\n\nif __name__ == \"__main__\":","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/project_euler/problem_005/sol1.py#L33-L69","documentation":"Documented TypeError from project_euler/problem_005/sol1.py:solution when int(n) fails (solution('asd')). CAVEAT: the guard is Python 2 syntax ('except TypeError, ValueError:'), so under Python 3 the module raises SyntaxError at import and this TypeError is unreachable until the clause is parenthesized.","triggerScenarios":"After the fix: solution('asd'), solution(None). Today: importing or running project_euler/problem_005/sol1.py under Python 3 fails with SyntaxError('multiple exception types must be parenthesized') before any TypeError can be raised.","commonSituations":"py3 doctest/test sweeps across the repo; passing a str n from CLI parsing without converting; migrating old checkouts where all sol*.py files share this py2 idiom.","solutions":["Fix 'except TypeError, ValueError:' to 'except (TypeError, ValueError):' at project_euler/problem_005/sol1.py:51.","Then pass int or int-castable n (e.g. 20).","Convert CLI args with int(sys.argv[1]) before calling.","Compile-check legacy modules in 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":["Fix the py2 except clause at problem_005/sol1.py:51 - module is currently unimportable under py3.","Convert CLI strings with int() before calling.","Compile-check legacy euler modules."],"tags":["python-euler","python2-syntax","typeerror","validation","lcm"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}