{"record":{"id":"4a420f04ca6c29e8","repo":"geekcomputers/Python","slug":"parameter-n-must-be-int-or-passive-of-cast-to-int","errorCode":null,"errorMessage":"Parameter n must be int or passive of cast to int.","messagePattern":"Parameter n must be int or passive of cast to int\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"A solution to project euler problem 3.py","lineNumber":41,"sourceCode":"        ...\n    ValueError: Parameter n must be greater or equal to one.\n    >>> solution(-17)\n    Traceback (most recent call last):\n        ...\n    ValueError: Parameter n must be greater or equal to one.\n    >>> solution([])\n    Traceback (most recent call last):\n        ...\n    TypeError: Parameter n must be int or passive of cast to int.\n    >>> solution(\"asd\")\n    Traceback (most recent call last):\n        ...\n    TypeError: Parameter n must be int or passive of cast to int.\n    \"\"\"\n    try:\n        n = int(n)\n    except (TypeError, ValueError):\n        raise TypeError(\"Parameter n must be int or passive of cast to int.\")\n    if n <= 0:\n        raise ValueError(\"Parameter n must be greater or equal to one.\")\n\n    i = 2\n    ans = 0\n\n    if n == 2:\n        return 2\n\n    while n > 2:\n        while n % i != 0:\n            i += 1\n\n        ans = i\n\n        while n % i == 0:\n            n = n / i\n","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/geekcomputers/Python/blob/40f4cd2652d75ef8e49d76e5c4d431d458712719/A solution to project euler problem 3.py#L23-L59","documentation":"Raised by the solution() function when its parameter n cannot be converted to an int (int(n) raises TypeError or ValueError). The function validates input before computing the largest prime factor of n. The 'passive of cast' wording is a typo for 'capable of being cast'.","triggerScenarios":"Calling solution(n) with a non-numeric argument such as a string like \"abc\", None, or a list. Strings like \"17\" do NOT trigger it because int(\"17\") succeeds.","commonSituations":"Passing user input from input() without conversion, passing None from an optional variable, or calling with a float like 1.5 expecting truncation (floats actually pass the cast).","solutions":["Pass an int or an int-parsable string, e.g. solution(600851475143) or solution(\"600851475143\")","If input comes from a user, convert/validate first: n = int(input_text) inside try/except","Convert or reject None before calling"],"exampleFix":"// before\nsolution(\"12ab\")\n// after\nsolution(600851475143)","handlingStrategy":"validation","validationCode":"def is_int_like(n):\n    if isinstance(n, bool):\n        return False\n    if isinstance(n, int):\n        return True\n    if isinstance(n, str):\n        return n.strip().lstrip('+-').isdigit()\n    return False\n\nif not is_int_like(n):\n    raise TypeError('n must be int-like') from None","typeGuard":"def is_int_like(n) -> TypeGuard[int]:\n    return (isinstance(n, int) and not isinstance(n, bool)) or (isinstance(n, str) and n.strip().lstrip('+-').isdigit())","tryCatchPattern":"try:\n    solution(n)\nexcept TypeError as e:\n    if 'cast to int' in str(e):\n        n = int(input('enter an integer: '))\n    else:\n        raise","preventionTips":["Convert user input to int at the boundary","Never pass None or containers to numeric APIs","Use isinstance checks before calling math utilities"],"tags":["python","type-error","input-validation","project-euler"],"backgroundTag":"invalid-argument-type","analyzedSha":"40f4cd2652d75ef8e49d76e5c4d431d458712719","analyzedAt":"2026-08-27T11:12:20.313Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}