TheAlgorithms/Python · error · ValueError
'base' must be between 2 and 36 inclusive
Error message
'base' must be between 2 and 36 inclusive
What it means
Raised by int_to_base() in maths/special_numbers/harshad_numbers.py when the base argument is outside [2, 36]. The function converts a positive integer to its string representation in the given base using the digit alphabet '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', which only defines 36 digits, so bases below 2 (no positional radix) or above 36 (no digit symbols) are meaningless.
Source
Thrown at maths/special_numbers/harshad_numbers.py:38
>>> int_to_base(167, 16)
'A7'
>>> # bases below 2 and beyond 36 will error
>>> int_to_base(98, 1)
Traceback (most recent call last):
...
ValueError: 'base' must be between 2 and 36 inclusive
>>> int_to_base(98, 37)
Traceback (most recent call last):
...
ValueError: 'base' must be between 2 and 36 inclusive
>>> int_to_base(-99, 16)
Traceback (most recent call last):
...
ValueError: number must be a positive integer
"""
if base < 2 or base > 36:
raise ValueError("'base' must be between 2 and 36 inclusive")
digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
result = ""
if number < 0:
raise ValueError("number must be a positive integer")
while number > 0:
number, remainder = divmod(number, base)
result = digits[remainder] + result
if result == "":
result = "0"
return result
def sum_of_digits(num: int, base: int) -> str:View on GitHub (pinned to f5988cc097)
Solutions
- Clamp or reject the base before calling: only call when 2 <= base <= 36
- If you need base-2-only logic, hard-code base=2 instead of parameterizing
- Validate user input for base at the API boundary with a clear error of your own
Example fix
// before
s = int_to_base(98, 37) # ValueError
// after
base = 37
if not 2 <= base <= 36:
raise ValueError(f"unsupported base {base}")
s = int_to_base(98, base) Defensive patterns
Strategy: validation
Validate before calling
def safe_int_to_base(number: int, base: int) -> str:
if not 2 <= base <= 36:
raise ValueError(f"base {base} out of range 2-36")
return int_to_base(number, base) Try / catch
try:
s = int_to_base(n, base)
except ValueError as e:
if 'base' in str(e):
s = int_to_base(n, 10) # fall back to decimal
else:
raise Prevention
- Validate base once at your boundary and reuse the validated value
- Use range(2, 37) when sweeping bases
- Write a unit test for base=1 and base=37 boundaries
When it happens
Trigger: Calling int_to_base(number, base) with base < 2 (e.g. base=1 or base=0) or base > 36 (e.g. base=37). The check runs before any conversion, so even valid numbers like int_to_base(98, 37) fail immediately.
Common situations: Passing a user-supplied radix without clamping, off-by-one loops like `for b in range(2, 37)` miswritten as `range(2, 38)`, or assuming the function supports arbitrary bases like base64.
Related errors
- surface_area_cube() only accepts non-negative values
- surface_area_cuboid() only accepts non-negative values
- surface_area_sphere() only accepts non-negative values
- surface_area_hemisphere() only accepts non-negative values
- surface_area_cone() only accepts non-negative values
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/b1ff12d90ff6ba18.
Report an issue: GitHub.