TheAlgorithms/Python · error · ValueError

n must be greater than 0. Got n = {number}

Error message

n must be greater than 0. Got n = {number}

What it means

Raised as ValueError by min_steps_to_one(number) when number <= 0. The DP builds table = [number + 1] * (number + 1) and seeds table[1], so zero or negative n would produce an empty/invalid table and IndexError downstream; the guard rejects them first. The message interpolates the offending n ('Got n = {number}').

Source

Thrown at dynamic_programming/minimum_steps_to_one.py:46


def min_steps_to_one(number: int) -> int:
    """
    Minimum steps to 1 implemented using tabulation.
    >>> min_steps_to_one(10)
    3
    >>> min_steps_to_one(15)
    4
    >>> min_steps_to_one(6)
    2

    :param number:
    :return int:
    """

    if number <= 0:
        msg = f"n must be greater than 0. Got n = {number}"
        raise ValueError(msg)

    table = [number + 1] * (number + 1)

    # starting position
    table[1] = 0
    for i in range(1, number):
        table[i + 1] = min(table[i + 1], table[i] + 1)
        # check if out of bounds
        if i * 2 <= number:
            table[i * 2] = min(table[i * 2], table[i] + 1)
        # check if out of bounds
        if i * 3 <= number:
            table[i * 3] = min(table[i * 3], table[i] + 1)
    return table[number]


if __name__ == "__main__":
    import doctest

View on GitHub (pinned to f5988cc097)

Solutions

  1. Check n >= 1 before calling; treat n == 0 as invalid input at your own boundary with a clearer message.
  2. Fix the length arithmetic that yields 0 or negative (e.g. use max(1, n) only if a default is acceptable).
  3. Read the 'Got n = ...' part of the message to trace the bad value's origin.

Example fix

# before
steps = min_steps_to_one(len(queue) - 1)  # empty queue -> 0 -> ValueError

# after
steps = min_steps_to_one(len(queue) - 1) if len(queue) >= 2 else 0
Defensive patterns

Strategy: validation

Validate before calling

if not isinstance(number, int) or number < 1:
    raise ValueError(f'n must be an integer >= 1, got {number!r}')
steps = min_steps_to_one(number)

Type guard

def is_positive_int(value: object) -> bool:
    return isinstance(value, int) and not isinstance(value, bool) and value >= 1

Try / catch

try:
    steps = min_steps_to_one(number)
except ValueError as exc:
    if 'n must be greater than 0' in str(exc):
        raise ValueError(f'invalid puzzle size {number}; must be >= 1') from exc
    raise

Prevention

When it happens

Trigger: min_steps_to_one(0) or min_steps_to_one(-3); n derived from len(collection) - 1 on an empty collection; passing a float like 1.5 works by luck of indexing but 0.0 raises here. The classic puzzle is defined only for n >= 1 (steps: n-1, n/2, n/3).

Common situations: Empty-input edge cases where the count becomes 0; off-by-one in loop bounds; unvalidated CLI parameters.

Related errors


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