geekcomputers/Python · error · TypeError

Parameter n must be int or passive of cast to int.

Error message

Parameter n must be int or passive of cast to int.

What it means

Raised by the solution() function when its parameter n cannot be converted to an int (int(n) raises TypeError or ValueError). The function validates input before computing the largest prime factor of n. The 'passive of cast' wording is a typo for 'capable of being cast'.

Source

Thrown at A solution to project euler problem 3.py:41

        ...
    ValueError: Parameter n must be greater or equal to one.
    >>> solution(-17)
    Traceback (most recent call last):
        ...
    ValueError: Parameter n must be greater or equal to one.
    >>> solution([])
    Traceback (most recent call last):
        ...
    TypeError: Parameter n must be int or passive of cast to int.
    >>> solution("asd")
    Traceback (most recent call last):
        ...
    TypeError: Parameter n must be int or passive of cast to int.
    """
    try:
        n = int(n)
    except (TypeError, ValueError):
        raise TypeError("Parameter n must be int or passive of cast to int.")
    if n <= 0:
        raise ValueError("Parameter n must be greater or equal to one.")

    i = 2
    ans = 0

    if n == 2:
        return 2

    while n > 2:
        while n % i != 0:
            i += 1

        ans = i

        while n % i == 0:
            n = n / i

View on GitHub (pinned to 40f4cd2652)

Solutions

  1. Pass an int or an int-parsable string, e.g. solution(600851475143) or solution("600851475143")
  2. If input comes from a user, convert/validate first: n = int(input_text) inside try/except
  3. Convert or reject None before calling

Example fix

// before
solution("12ab")
// after
solution(600851475143)
Defensive patterns

Strategy: validation

Validate before calling

def is_int_like(n):
    if isinstance(n, bool):
        return False
    if isinstance(n, int):
        return True
    if isinstance(n, str):
        return n.strip().lstrip('+-').isdigit()
    return False

if not is_int_like(n):
    raise TypeError('n must be int-like') from None

Type guard

def is_int_like(n) -> TypeGuard[int]:
    return (isinstance(n, int) and not isinstance(n, bool)) or (isinstance(n, str) and n.strip().lstrip('+-').isdigit())

Try / catch

try:
    solution(n)
except TypeError as e:
    if 'cast to int' in str(e):
        n = int(input('enter an integer: '))
    else:
        raise

Prevention

When it happens

Trigger: Calling solution(n) with a non-numeric argument such as a string like "abc", None, or a list. Strings like "17" do NOT trigger it because int("17") succeeds.

Common situations: Passing user input from input() without conversion, passing None from an optional variable, or calling with a float like 1.5 expecting truncation (floats actually pass the cast).

Understand the failure class

Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.

Related errors


AI-assisted analysis of geekcomputers/Python@40f4cd2652 (2026-08-27). Data as JSON: /api/errors/4a420f04ca6c29e8. Report an issue: GitHub.