TheAlgorithms/Python · error · TypeError

Parameters chain_length and number_limit must be int

Error message

Parameters chain_length and number_limit must be int

What it means

Raised by solution() in project_euler/problem_074/sol2.py when either chain_length or number_limit is not an int. The function runs range(1, number_limit) and dict-based chain caching that assume plain integers; floats (even integral ones like 10.0, per the doctest) and strings are rejected with TypeError before any computation.

Source

Thrown at project_euler/problem_074/sol2.py:102

        ...
    TypeError: Parameters chain_length and number_limit must be int

    >>> solution(0, 1000)
    Traceback (most recent call last):
        ...
    ValueError: Parameters chain_length and number_limit must be greater than 0

    >>> solution(10, 0)
    Traceback (most recent call last):
        ...
    ValueError: Parameters chain_length and number_limit must be greater than 0

    >>> solution(10, 1000)
    26
    """

    if not isinstance(chain_length, int) or not isinstance(number_limit, int):
        raise TypeError("Parameters chain_length and number_limit must be int")

    if chain_length <= 0 or number_limit <= 0:
        raise ValueError(
            "Parameters chain_length and number_limit must be greater than 0"
        )

    # the counter for the chains with the exact desired length
    chains_counter = 0
    # the cached sizes of the previous chains
    chain_sets_lengths: dict[int, int] = {}

    for start_chain_element in range(1, number_limit):
        # The temporary set will contain the elements of the chain
        chain_set = set()
        chain_set_length = 0

        # Stop computing the chain when you find a cached size, a repeating item or the
        # length is greater then the desired one.

View on GitHub (pinned to f5988cc097)

Solutions

  1. Use int literals: solution(60, 1000000).
  2. With argparse, add type=int to the argument definition.
  3. Coerce near the call: solution(int(chain_length), int(number_limit)) once validated as integral.

Example fix

# before
solution(60, 1e6)  # TypeError: 1e6 is float

# after
solution(60, 1_000_000)
Defensive patterns

Strategy: type-guard

Validate before calling

for p in (chain_length, number_limit):
    if not isinstance(p, int) or isinstance(p, bool):
        raise TypeError(f"parameters must be int, got {p!r}")
solution(chain_length, number_limit)

Type guard

def are_int_params(*values) -> bool:
    return all(isinstance(v, int) and not isinstance(v, bool) for v in values)

Try / catch

try:
    count = solution(chain_length, number_limit)
except TypeError:
    count = solution(int(chain_length), int(number_limit))
except ValueError:
    raise  # non-positive params; fix config

Prevention

When it happens

Trigger: solution(10.0, 1000), solution("60", 1000000), solution(60, 1e6) (1e6 is float), solution(None, 100).

Common situations: Scientific-notation literals (1e6) in scripts; JSON/YAML config parsed floats; passing parameters via argparse without type=int.

Related errors


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