{"record":{"id":"625047c4e83ddae9","repo":"TheAlgorithms/Python","slug":"n-must-be-greater-than-0-got-n-number","errorCode":null,"errorMessage":"n must be greater than 0. Got n = {number}","messagePattern":"n must be greater than 0\\. Got n = (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"dynamic_programming/minimum_steps_to_one.py","lineNumber":46,"sourceCode":"\n\ndef min_steps_to_one(number: int) -> int:\n    \"\"\"\n    Minimum steps to 1 implemented using tabulation.\n    >>> min_steps_to_one(10)\n    3\n    >>> min_steps_to_one(15)\n    4\n    >>> min_steps_to_one(6)\n    2\n\n    :param number:\n    :return int:\n    \"\"\"\n\n    if number <= 0:\n        msg = f\"n must be greater than 0. Got n = {number}\"\n        raise ValueError(msg)\n\n    table = [number + 1] * (number + 1)\n\n    # starting position\n    table[1] = 0\n    for i in range(1, number):\n        table[i + 1] = min(table[i + 1], table[i] + 1)\n        # check if out of bounds\n        if i * 2 <= number:\n            table[i * 2] = min(table[i * 2], table[i] + 1)\n        # check if out of bounds\n        if i * 3 <= number:\n            table[i * 3] = min(table[i * 3], table[i] + 1)\n    return table[number]\n\n\nif __name__ == \"__main__\":\n    import doctest","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/dynamic_programming/minimum_steps_to_one.py#L28-L64","documentation":"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}').","triggerScenarios":"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).","commonSituations":"Empty-input edge cases where the count becomes 0; off-by-one in loop bounds; unvalidated CLI parameters.","solutions":["Check n >= 1 before calling; treat n == 0 as invalid input at your own boundary with a clearer message.","Fix the length arithmetic that yields 0 or negative (e.g. use max(1, n) only if a default is acceptable).","Read the 'Got n = ...' part of the message to trace the bad value's origin."],"exampleFix":"# before\nsteps = min_steps_to_one(len(queue) - 1)  # empty queue -> 0 -> ValueError\n\n# after\nsteps = min_steps_to_one(len(queue) - 1) if len(queue) >= 2 else 0","handlingStrategy":"validation","validationCode":"if not isinstance(number, int) or number < 1:\n    raise ValueError(f'n must be an integer >= 1, got {number!r}')\nsteps = min_steps_to_one(number)","typeGuard":"def is_positive_int(value: object) -> bool:\n    return isinstance(value, int) and not isinstance(value, bool) and value >= 1","tryCatchPattern":"try:\n    steps = min_steps_to_one(number)\nexcept ValueError as exc:\n    if 'n must be greater than 0' in str(exc):\n        raise ValueError(f'invalid puzzle size {number}; must be >= 1') from exc\n    raise","preventionTips":["Guard count-derived indices (len(x) - k) against zero/negative results.","Validate puzzle/problem sizes at the input boundary with clearer domain messages.","Read the interpolated n in the message to trace which caller passed the bad value."],"tags":["python","input-validation","boundary-check","dynamic-programming"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}