{"record":{"id":"f1368ab5c24daa33","repo":"TheAlgorithms/Python","slug":"invalid-input-f1368a","errorCode":null,"errorMessage":"Invalid input","messagePattern":"Invalid input","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"project_euler/problem_097/sol1.py","lineNumber":36,"sourceCode":"    >>> solution(8)\r\n    '39992577'\r\n    >>> solution(1)\r\n    '7'\r\n    >>> solution(-1)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: Invalid input\r\n    >>> solution(8.3)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: Invalid input\r\n    >>> solution(\"a\")\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: Invalid input\r\n    \"\"\"\r\n    if not isinstance(n, int) or n < 0:\r\n        raise ValueError(\"Invalid input\")\r\n    modulus = 10**n\r\n    number = 28433 * (pow(2, 7830457, modulus)) + 1\r\n    return str(number % modulus)\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    from doctest import testmod\r\n\r\n    testmod()\r\n    print(f\"{solution(10) = }\")\r\n","sourceCodeStart":18,"sourceCodeEnd":47,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/project_euler/problem_097/sol1.py#L18-L47","documentation":"Raised by solution() in project_euler/problem_097/sol1.py when n is not an int or is negative. n selects how many last digits to keep via modulus = 10**n, so it must be a non-negative integer; a single combined isinstance-and-range check rejects floats, strings, and negatives alike with the generic message 'Invalid input'.","triggerScenarios":"solution(8.3), solution(\"a\"), solution(-1), solution(None). Also solution(3.0) because isinstance(3.0, int) is False, even though the value is integral.","commonSituations":"CLI arguments passed as strings (argparse without type=int); JSON config with 10.0; computed digit counts that end up as floats (e.g. math.log results).","solutions":["Pass a plain non-negative int: solution(10).","Use argparse with type=int for the digits parameter.","Coerce integral floats before calling: solution(int(n)) when float(n).is_integer().","Because the message is generic, log the offending value yourself when validating."],"exampleFix":"# before\nn = float(digits_arg)  # e.g. 10.0 from JSON\nlast = solution(n)  # ValueError: Invalid input\n\n# after\nn = int(digits_arg)\nlast = solution(n)","handlingStrategy":"validation","validationCode":"if not isinstance(n, int) or isinstance(n, bool) or n < 0:\n    raise ValueError(f\"n must be a non-negative int, got {n!r}\")\nsolution(n)","typeGuard":"def is_valid_digit_count(value) -> bool:\n    return isinstance(value, int) and not isinstance(value, bool) and value >= 0","tryCatchPattern":"try:\n    tail = solution(n)\nexcept ValueError:\n    # generic message; re-raise with context\n    raise ValueError(f\"invalid digit count: {n!r}\") from None","preventionTips":["Use argparse type=int; convert JSON numbers with int() when integral.","The message 'Invalid input' is generic, so include the value in your own errors.","Note ints only: 3.0 fails the isinstance check."],"tags":["project-euler","validation","valueerror","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}