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_003/sol3.py:solution when int(n) fails (solution('asd')). CAVEAT: the file's validation uses Python 2 syntax 'except TypeError, ValueError:' and therefore raises SyntaxError under Python 3 at import time - the documented TypeError cannot fire until the clause is rewritten as 'except (TypeError, ValueError):'.
Source
Thrown at project_euler/problem_003/sol3.py:48
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 = 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
i += 1
return int(ans)
if __name__ == "__main__":
print(f"{solution() = }")View on GitHub (pinned to f5988cc097)
Solutions
- Parenthesize the exceptions at project_euler/problem_003/sol3.py:48.
- Pass int or int-castable values afterwards.
- Guard calls with a small is_valid_n helper shared across the problem_003 variants.
- Add compile checks for legacy modules to 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
- Parenthesize the except clause at problem_003/sol3.py:48 first.
- Share one input-validation helper across all problem_003 variants.
- Compile-check legacy modules in CI.
When it happens
Trigger: After the syntax fix: solution('asd'), solution(None), solution(3.5) is fine (casts), solution(object()) raises. Today: running or importing the module under Python 3 produces SyntaxError('multiple exception types must be parenthesized').
Common situations: Whole-repo doctest sweeps under Python 3; migration of an old algorithms checkout; harnesses that call every problem_003 solution variant with the same inputs, where sol1/sol2/sol3 all share the same broken guard.
Related errors
- Parameter n must be int or castable to int.
- Parameter n must be int or castable to int.
- Parameter n must be int or castable to int.
- Parameter n must be greater than or equal to one.
- Parameter n must be greater than or equal to one.
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/e81f5115a71431e2.
Report an issue: GitHub.