TheAlgorithms/Python · error · TypeError

Input value of [number={number}] must be an integer

Error message

Input value of [number={number}] must be an integer

What it means

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.

Source

Thrown at maths/twin_prime.py:36

    returns n+2 if n and n+2 are prime numbers and -1 otherwise.
    >>> twin_prime(3)
    5
    >>> twin_prime(4)
    -1
    >>> twin_prime(5)
    7
    >>> twin_prime(17)
    19
    >>> twin_prime(0)
    -1
    >>> twin_prime(6.0)
    Traceback (most recent call last):
        ...
    TypeError: Input value of [number=6.0] must be an integer
    """
    if not isinstance(number, int):
        msg = f"Input value of [number={number}] must be an integer"
        raise TypeError(msg)
    if is_prime(number) and is_prime(number + 2):
        return number + 2
    else:
        return -1


if __name__ == "__main__":
    import doctest

    doctest.testmod()

View on GitHub (pinned to f5988cc097)

Solutions

  1. Coerce integral floats before calling: twin_prime(int(x))
  2. Keep prime-candidate pipelines in integer arithmetic
  3. Add your own isinstance(n, int) guard at the boundary

Example fix

// before
tp = twin_prime(6.0)  # TypeError

// after
tp = twin_prime(int(6.0))  # -1, 6 is not part of a twin-prime pair
Defensive patterns

Strategy: type-guard

Validate before calling

if not isinstance(n, int) or isinstance(n, bool):
    n = int(n)
print(twin_prime(n))

Type guard

def is_builtin_int(v) -> TypeGuard[int]:
    return isinstance(v, int) and not isinstance(v, bool)

Try / catch

try:
    tp = twin_prime(n)
except TypeError:
    tp = twin_prime(int(n))

Prevention

When it happens

Trigger: 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.

Common situations: Candidates sourced from float-heavy pipelines or JSON; forwarding values from untyped function parameters; numpy int scalars under some versions.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/0265576c26811b1b. Report an issue: GitHub.