TheAlgorithms/Python · error · ValueError
solution() only accepts values from 0 to 100
Error message
solution() only accepts values from 0 to 100
What it means
Raised by solution() in project_euler/problem_112/sol1.py when percent is not strictly between 0 and 100 (check: not 0 < percent < 100). The function searches for the least number whose bouncy proportion reaches percent, which is only well-defined inside the open interval; 0, 100, and out-of-range values are rejected. Floats are accepted here, unlike the int-only helpers.
Source
Thrown at project_euler/problem_112/sol1.py:75
Returns the least number for which the proportion of bouncy numbers is
exactly 'percent'
>>> solution(50)
538
>>> solution(90)
21780
>>> solution(80)
4770
>>> solution(105)
Traceback (most recent call last):
...
ValueError: solution() only accepts values from 0 to 100
>>> solution(100.011)
Traceback (most recent call last):
...
ValueError: solution() only accepts values from 0 to 100
"""
if not 0 < percent < 100:
raise ValueError("solution() only accepts values from 0 to 100")
bouncy_num = 0
num = 1
while True:
if check_bouncy(num):
bouncy_num += 1
if (bouncy_num / num) * 100 >= percent:
return num
num += 1
if __name__ == "__main__":
from doctest import testmod
testmod()
print(f"{solution(99)}")
View on GitHub (pinned to f5988cc097)
Solutions
- Pass a value strictly inside (0, 100), e.g. solution(99) for the canonical problem.
- If your input is a fraction (0-1), multiply by 100 first: solution(frac * 100).
- Validate range in your UI/config layer: if not 0 < pct < 100: reject.
- For '100% bouncy' style requests, cap to 99.999 or handle as a special case outside this API.
Example fix
# before pct = 100 # user asked for 'all bouncy' solution(pct) # ValueError # after pct = min(pct, 99.999) solution(pct)
Defensive patterns
Strategy: validation
Validate before calling
if not 0 < percent < 100:
raise ValueError(f"percent must be in (0, 100) exclusive, got {percent}")
solution(percent) Type guard
def is_valid_percent(p) -> bool:
return isinstance(p, (int, float)) and 0 < p < 100 Try / catch
try:
num = solution(percent)
except ValueError as e:
if "0 to 100" in str(e):
num = solution(min(max(percent, 0.001), 99.999))
else:
raise Prevention
- The interval is open: 0 and 100 are rejected; clamp inside (0, 100).
- If your value is a fraction, multiply by 100 before calling.
- Validate percentage fields in config/UI with an exclusive range.
When it happens
Trigger: solution(105), solution(100.011), solution(0), solution(100), solution(-5). Note the endpoints themselves are rejected: solution(100) raises even though 100 looks reasonable (the proportion never provably reaches exactly 100% in the incremental search).
Common situations: User-supplied percentage fields without range validation; passing 100 expecting 'all bouncy'; unit tests probing boundary values; sign errors or percent-vs-fraction confusion (passing 0.99 instead of 99).
Related errors
- Parameter nth must be greater than or equal to one.
- n must be >= 0
- Hand should contain only 5 cards: {hand!r}
- gon_side must be in the range [3, 5]
- a gon ring should have a length that is a multiple of 3
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/2ea73eb908542521.
Report an issue: GitHub.