TheAlgorithms/Python · critical · TypeError
Parameter nth must be int or castable to int.
Error message
Parameter nth must be int or castable to int.
What it means
Documented TypeError from project_euler/problem_007/sol2.py:solution when int(nth) fails (solution('asd')). The raise even uses 'from None' (correct py3 style) but the except clause itself is py2 syntax ('except TypeError, ValueError:'), so under Python 3 the module fails to import and this TypeError is unreachable until the clause is fixed.
Source
Thrown at project_euler/problem_007/sol2.py:91
ValueError: Parameter nth must be greater than or equal to one.
>>> solution(-17)
Traceback (most recent call last):
...
ValueError: Parameter nth must be greater than or equal to one.
>>> solution([])
Traceback (most recent call last):
...
TypeError: Parameter nth must be int or castable to int.
>>> solution("asd")
Traceback (most recent call last):
...
TypeError: Parameter nth must be int or castable to int.
"""
try:
nth = int(nth)
except TypeError, ValueError:
raise TypeError("Parameter nth must be int or castable to int.") from None
if nth <= 0:
raise ValueError("Parameter nth must be greater than or equal to one.")
primes: list[int] = []
num = 2
while len(primes) < nth:
if is_prime(num):
primes.append(num)
num += 1
else:
num += 1
return primes[len(primes) - 1]
if __name__ == "__main__":
print(f"{solution() = }")
View on GitHub (pinned to f5988cc097)
Solutions
- Change 'except TypeError, ValueError:' to 'except (TypeError, ValueError):' at project_euler/problem_007/sol2.py:91.
- Then pass int or int-castable nth (e.g. 10001).
- Validate nth with isinstance or isdigit checks in callers.
- Run ruff/flake8 or py_compile over the module to catch the syntax class of error automatically.
Example fix
# before
except TypeError, ValueError:
raise TypeError('Parameter nth must be int or castable to int.') from None
# after
except (TypeError, ValueError):
raise TypeError('Parameter nth must be int or castable to int.') from None Defensive patterns
Strategy: type-guard
Validate before calling
nth = int(nth)
if nth <= 0:
raise ValueError('nth must be >= 1')
solution(nth) Type guard
def is_valid_prime_index(nth) -> bool:
return isinstance(nth, int) and not isinstance(nth, bool) and nth >= 1 Try / catch
try:
solution(nth)
except TypeError as e:
if 'castable' in str(e):
... Prevention
- Fix 'except TypeError, ValueError:' at problem_007/sol2.py:91 to parenthesized form - the file mixes py3 annotations with a py2 except, so the SyntaxError surprises people.
- Validate nth (a positive index, not a prime value) before calling.
- Use ruff/py_compile in CI to catch mixed py2 syntax.
When it happens
Trigger: After the fix: solution('asd'), solution(None). Today: importing or executing project_euler/problem_007/sol2.py under Python 3 raises SyntaxError('multiple exception types must be parenthesized') at line 91 instead.
Common situations: py3 test sweeps over the repo's euler solutions; unconverted CLI or query-string parameters reaching solution(); mixed py2/py3 idioms in one file (this file also uses modern 'list[int]' annotations, which makes the py2 except clause easy to miss).
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 int or castable to int.
- Parameter n must be int or castable to int.
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/2534c1752de2c40c.
Report an issue: GitHub.