TheAlgorithms/Python · critical · ValueError

Parameter n must be greater than or equal to one.

Error message

Parameter n must be greater than or equal to one.

What it means

Documented ValueError from project_euler/problem_002/sol4.py:solution when n <= 0 after successful int conversion. The Fibonacci-derived formula needs a positive upper bound. CAVEAT: the surrounding try/except uses Python 2 syntax ('except TypeError, ValueError:'), so under Python 3 the module dies with SyntaxError at import and this ValueError is unreachable until the syntax is fixed.

Source

Thrown at project_euler/problem_002/sol4.py:62

    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.")
    getcontext().prec = 100
    phi = (Decimal(5) ** Decimal("0.5") + 1) / Decimal(2)

    index = (math.floor(math.log(n * (phi + 2), phi) - 1) // 3) * 3 + 2
    num = Decimal(round(phi ** Decimal(index + 1))) / (phi + 2)
    total = num // 2
    return int(total)


if __name__ == "__main__":
    print(f"{solution() = }")

View on GitHub (pinned to f5988cc097)

Solutions

  1. Fix 'except TypeError, ValueError:' to 'except (TypeError, ValueError):' at project_euler/problem_002/sol4.py:60 so the module imports at all.
  2. Pass n >= 1 (the smallest valid Fibonacci-even-sum bound).
  3. Pre-validate n >= 1 at the call site for harness code that iterates over many n values.
  4. Catch ValueError to skip invalid bounds in batch runs.

Example fix

# before
solution(0)

# after
if n >= 1:
    result = solution(n)
Defensive patterns

Strategy: validation

Validate before calling

n = int(n)
if n <= 0:
    raise ValueError('n must be >= 1')
solution(n)

Type guard

def is_valid_fib_bound(n) -> bool:
    return isinstance(n, int) and not isinstance(n, bool) and n >= 1

Try / catch

try:
    solution(n)
except ValueError as e:
    if 'greater than or equal to one' in str(e):
        ...

Prevention

When it happens

Trigger: After fixing the syntax: solution(0), solution(-5), or solution('0') (string casts fine, then fails the > 0 check). As written today: importing the module under Python 3 raises SyntaxError before this check can ever run.

Common situations: Passing 0 as a 'no limit' sentinel; off-by-one bugs producing 0 from max(k-1, 0)-style computations; empty-input edge cases in a generic harness that calls every Project Euler solution with n=0; running under Python 3 where the file's py2 except syntax breaks first.

Related errors


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