{"record":{"id":"0265576c26811b1b","repo":"TheAlgorithms/Python","slug":"input-value-of-number-number-must-be-an-intege-026557","errorCode":null,"errorMessage":"Input value of [number={number}] must be an integer","messagePattern":"Input value of \\[number=(.+?)\\] must be an integer","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"maths/twin_prime.py","lineNumber":36,"sourceCode":"    returns n+2 if n and n+2 are prime numbers and -1 otherwise.\n    >>> twin_prime(3)\n    5\n    >>> twin_prime(4)\n    -1\n    >>> twin_prime(5)\n    7\n    >>> twin_prime(17)\n    19\n    >>> twin_prime(0)\n    -1\n    >>> twin_prime(6.0)\n    Traceback (most recent call last):\n        ...\n    TypeError: Input value of [number=6.0] must be an integer\n    \"\"\"\n    if not isinstance(number, int):\n        msg = f\"Input value of [number={number}] must be an integer\"\n        raise TypeError(msg)\n    if is_prime(number) and is_prime(number + 2):\n        return number + 2\n    else:\n        return -1\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":18,"sourceCodeEnd":47,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/twin_prime.py#L18-L47","documentation":"Raised by twin_prime(number) in maths/twin_prime.py when the argument is not an int. The function checks is_prime(number) and is_prime(number + 2) and returns number+2 for the smaller member of a twin-prime pair, or -1 otherwise; exact integer arithmetic is required, so floats like 6.0 are rejected by the strict isinstance check.","triggerScenarios":"Calling twin_prime(6.0), twin_prime('7'), or twin_prime(None). twin_prime(0) does not raise — it returns -1; negative ints also return -1 rather than erroring.","commonSituations":"Candidates sourced from float-heavy pipelines or JSON; forwarding values from untyped function parameters; numpy int scalars under some versions.","solutions":["Coerce integral floats before calling: twin_prime(int(x))","Keep prime-candidate pipelines in integer arithmetic","Add your own isinstance(n, int) guard at the boundary"],"exampleFix":"// before\ntp = twin_prime(6.0)  # TypeError\n\n// after\ntp = twin_prime(int(6.0))  # -1, 6 is not part of a twin-prime pair","handlingStrategy":"type-guard","validationCode":"if not isinstance(n, int) or isinstance(n, bool):\n    n = int(n)\nprint(twin_prime(n))","typeGuard":"def is_builtin_int(v) -> TypeGuard[int]:\n    return isinstance(v, int) and not isinstance(v, bool)","tryCatchPattern":"try:\n    tp = twin_prime(n)\nexcept TypeError:\n    tp = twin_prime(int(n))","preventionTips":["Non-primes and 0 return -1 — they do not raise","Coerce integral floats at the boundary","Keep prime-candidate pipelines in ints"],"tags":["math","typeerror","type-checking","primes"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}