TheAlgorithms/Python · error · ValueError

check_bouncy() accepts only integer arguments

Error message

check_bouncy() accepts only integer arguments

What it means

Raised by check_bouncy() in project_euler/problem_112/sol1.py when n is not an int instance. The function stringifies n and compares against its sorted characters, which is only meaningful for integers; floats and strings are rejected. Note the exception is ValueError, not TypeError, despite being a type check.

Source

Thrown at project_euler/problem_112/sol1.py:49

        ...
    ValueError: check_bouncy() accepts only integer arguments
    >>> check_bouncy(132475)
    True
    >>> check_bouncy(34)
    False
    >>> check_bouncy(341)
    True
    >>> check_bouncy(47)
    False
    >>> check_bouncy(-12.54)
    Traceback (most recent call last):
        ...
    ValueError: check_bouncy() accepts only integer arguments
    >>> check_bouncy(-6548)
    True
    """
    if not isinstance(n, int):
        raise ValueError("check_bouncy() accepts only integer arguments")
    str_n = str(n)
    sorted_str_n = "".join(sorted(str_n))
    return str_n not in {sorted_str_n, sorted_str_n[::-1]}


def solution(percent: float = 99) -> int:
    """
    Returns the least number for which the proportion of bouncy numbers is
    exactly 'percent'
    >>> solution(50)
    538
    >>> solution(90)
    21780
    >>> solution(80)
    4770
    >>> solution(105)
    Traceback (most recent call last):
        ...

View on GitHub (pinned to f5988cc097)

Solutions

  1. Pass plain ints: check_bouncy(341).
  2. Keep loop counters as ints; never divide-and-reassign the counter itself.
  3. Convert numeric inputs once at the boundary: n = int(n) after an integrality check.

Example fix

# before
num = 1.0  # float seed
while ...:
    check_bouncy(num)  # ValueError once reached
    num += 1

# after
num = 1  # int seed
Defensive patterns

Strategy: type-guard

Validate before calling

if not isinstance(n, int) or isinstance(n, bool):
    raise ValueError(f"check_bouncy needs int, got {type(n).__name__}")
check_bouncy(n)

Type guard

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

Try / catch

try:
    bouncy = check_bouncy(num)
except ValueError as e:
    if "integer arguments" in str(e):
        bouncy = check_bouncy(int(num))
    else:
        raise

Prevention

When it happens

Trigger: check_bouncy(-12.54) (the doctest case), check_bouncy("341"), check_bouncy(3.0), check_bouncy(None). Booleans pass because bool subclasses int.

Common situations: Counting loops that mix float arithmetic into the counter; values from pandas/numpy columns; stringified numbers from text processing.

Related errors


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