{"record":{"id":"b80c644851086a5c","repo":"TheAlgorithms/Python","slug":"parameter-n-must-be-int-or-castable-to-int","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_002/sol4.py","lineNumber":60,"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    getcontext().prec = 100\n    phi = (Decimal(5) ** Decimal(\"0.5\") + 1) / Decimal(2)\n\n    index = (math.floor(math.log(n * (phi + 2), phi) - 1) // 3) * 3 + 2\n    num = Decimal(round(phi ** Decimal(index + 1))) / (phi + 2)\n    total = num // 2\n    return int(total)\n\n\nif __name__ == \"__main__\":\n    print(f\"{solution() = }\")\n","sourceCodeStart":42,"sourceCodeEnd":74,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/project_euler/problem_002/sol4.py#L42-L74","documentation":"Documented TypeError from project_euler/problem_002/sol4.py:solution when n cannot be converted with int(n) (e.g. 'asd', None). CRITICAL CAVEAT: the guard is written as 'except TypeError, ValueError:', which is Python 2 syntax - this file raises SyntaxError at import under Python 3, so this TypeError is currently unreachable until the except clause is fixed to 'except (TypeError, ValueError):'.","triggerScenarios":"After fixing the syntax: calling solution('asd'), solution(None), or any object where int(n) raises. As written today: merely importing or running the module under Python 3 raises SyntaxError('multiple exception types must be parenthesized') before any call.","commonSituations":"Running the repository's Python-2-era solution files under Python 3 (the repo predates the py2->py3 migration of these guards); doctest suites that fail with SyntaxError instead of the documented TypeError; copy-pasting this validation idiom into new code.","solutions":["Fix the syntax first: change 'except TypeError, ValueError:' to 'except (TypeError, ValueError):' in project_euler/problem_002/sol4.py:60.","After the fix, pass an int or an int-castable value such as '100' or 100.0.","Pre-validate with isinstance(n, int) or str(n).isdigit() before calling.","Add the module to a doctest/CI run so the SyntaxError cannot regress."],"exampleFix":"# before (Python 2 syntax - SyntaxError under Python 3)\ntry:\n    n = int(n)\nexcept TypeError, ValueError:\n    raise TypeError('Parameter n must be int or castable to int.')\n\n# after\ntry:\n    n = int(n)\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).lstrip('-').isdigit():\n    raise TypeError(f'n must be int-castable, got {n!r}')\nsolution(n)","typeGuard":"def is_int_castable(v) -> bool:\n    if isinstance(v, bool):\n        return False\n    if isinstance(v, (int, float)):\n        return float(v).is_integer() or isinstance(v, int)\n    try:\n        int(v)\n        return True\n    except (TypeError, ValueError):\n        return False","tryCatchPattern":"try:\n    solution(n)\nexcept TypeError as e:\n    if 'castable' in str(e):\n        ...","preventionTips":["First make the module importable: fix 'except TypeError, ValueError:' to parenthesized form.","Convert CLI/query input to int at the boundary.","Run python -m py_compile on legacy euler modules in CI."],"tags":["python-euler","python2-syntax","typeerror","validation","migration"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}