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 is_automorphic_number() in maths/special_numbers/automorphic_number.py when number is not an int. An automorphic number's square ends in the number itself (e.g. 76^2 = 5776), and the digit-by-digit modulo comparison requires integer arithmetic, so floats — even whole ones like 5.0 — are rejected with TypeError rather than coerced.

Source

Thrown at maths/special_numbers/automorphic_number.py:44

    True
    >>> is_automorphic_number(7)
    False
    >>> is_automorphic_number(25)
    True
    >>> is_automorphic_number(259918212890625)
    True
    >>> is_automorphic_number(259918212890636)
    False
    >>> is_automorphic_number(740081787109376)
    True
    >>> is_automorphic_number(5.0)
    Traceback (most recent call last):
        ...
    TypeError: Input value of [number=5.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 number < 0:
        return False
    number_square = number * number
    while number > 0:
        if number % 10 != number_square % 10:
            return False
        number //= 10
        number_square //= 10
    return True


if __name__ == "__main__":
    import doctest

    doctest.testmod()

View on GitHub (pinned to f5988cc097)

Solutions

  1. Convert whole floats/strings explicitly: is_automorphic_number(int(x)) when x.is_integer()
  2. Use floor division // instead of / when computing the argument
  3. Validate types at the data boundary (API/CLI) before processing

Example fix

# before
is_automorphic_number(value / 2)  # float -> TypeError

# after
is_automorphic_number(value // 2)  # int
Defensive patterns

Strategy: type-guard

Validate before calling

if isinstance(number, float) and number.is_integer():
    number = int(number)
if isinstance(number, int):
    print(is_automorphic_number(number))

Type guard

def is_int_number(value: object) -> bool:
    return isinstance(value, int) and not isinstance(value, bool)

Try / catch

try:
    is_automorphic_number(number)
except TypeError as e:
    if 'must be an integer' in str(e):
        number = int(number)

Prevention

When it happens

Trigger: Calling is_automorphic_number(5.0), is_automorphic_number('25'), or passing a bool (note: isinstance(True, int) is True in Python, so booleans are silently accepted). Division results like n/2 in Python 3 are floats and raise.

Common situations: Values flowing from JSON where integers were serialized as floats; results of division instead of floor division; string input not converted. Negative integers do NOT raise — they return False.

Related errors


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