TheAlgorithms/Python · critical · TypeError

Parameter n must be int or castable to int.

Error message

Parameter n must be int or castable to int.

What it means

Documented TypeError from project_euler/problem_005/sol1.py:solution when int(n) fails (solution('asd')). CAVEAT: the guard is Python 2 syntax ('except TypeError, ValueError:'), so under Python 3 the module raises SyntaxError at import and this TypeError is unreachable until the clause is parenthesized.

Source

Thrown at project_euler/problem_005/sol1.py:51

    ValueError: Parameter n must be greater than or equal to one.
    >>> solution(-17)
    Traceback (most recent call last):
        ...
    ValueError: Parameter n must be greater than or equal to one.
    >>> solution([])
    Traceback (most recent call last):
        ...
    TypeError: Parameter n must be int or castable to int.
    >>> solution("asd")
    Traceback (most recent call last):
        ...
    TypeError: Parameter n must be int or castable to int.
    """

    try:
        n = int(n)
    except TypeError, ValueError:
        raise TypeError("Parameter n must be int or castable to int.")
    if n <= 0:
        raise ValueError("Parameter n must be greater than or equal to one.")
    i = 0
    while 1:
        i += n * (n - 1)
        nfound = 0
        for j in range(2, n):
            if i % j != 0:
                nfound = 1
                break
        if nfound == 0:
            if i == 0:
                i = 1
            return i
    return None


if __name__ == "__main__":

View on GitHub (pinned to f5988cc097)

Solutions

  1. Fix 'except TypeError, ValueError:' to 'except (TypeError, ValueError):' at project_euler/problem_005/sol1.py:51.
  2. Then pass int or int-castable n (e.g. 20).
  3. Convert CLI args with int(sys.argv[1]) before calling.
  4. Compile-check legacy modules in CI.

Example fix

# before
except TypeError, ValueError:
    raise TypeError('Parameter n must be int or castable to int.')

# after
except (TypeError, ValueError):
    raise TypeError('Parameter n must be int or castable to int.') from None
Defensive patterns

Strategy: type-guard

Validate before calling

if not is_int_castable(n):
    raise TypeError(f'n must be int-castable, got {n!r}')
solution(n)

Type guard

def is_int_castable(v) -> bool:
    try:
        int(v)
        return True
    except (TypeError, ValueError):
        return False

Try / catch

try:
    solution(n)
except TypeError:
    ...

Prevention

When it happens

Trigger: After the fix: solution('asd'), solution(None). Today: importing or running project_euler/problem_005/sol1.py under Python 3 fails with SyntaxError('multiple exception types must be parenthesized') before any TypeError can be raised.

Common situations: py3 doctest/test sweeps across the repo; passing a str n from CLI parsing without converting; migrating old checkouts where all sol*.py files share this py2 idiom.

Related errors


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