{"record":{"id":"84b3e6ab4710e494","repo":"TheAlgorithms/Python","slug":"the-input-value-of-n-number-has-to-be-0","errorCode":null,"errorMessage":"The input value of [n={number}] has to be > 0","messagePattern":"The input value of \\[n=(.+?)\\] has to be > 0","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/sylvester_sequence.py","lineNumber":35,"sourceCode":"    113423713055421844361000443\n\n    >>> sylvester(-1)\n    Traceback (most recent call last):\n        ...\n    ValueError: The input value of [n=-1] has to be > 0\n\n    >>> sylvester(8.0)\n    Traceback (most recent call last):\n        ...\n    AssertionError: The input value of [n=8.0] is not an integer\n    \"\"\"\n    assert isinstance(number, int), f\"The input value of [n={number}] is not an integer\"\n\n    if number == 1:\n        return 2\n    elif number < 1:\n        msg = f\"The input value of [n={number}] has to be > 0\"\n        raise ValueError(msg)\n    else:\n        num = sylvester(number - 1)\n        lower = num - 1\n        upper = num\n        return lower * upper + 1\n\n\nif __name__ == \"__main__\":\n    print(f\"The 8th number in Sylvester's sequence: {sylvester(8)}\")\n","sourceCodeStart":17,"sourceCodeEnd":45,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/sylvester_sequence.py#L17-L45","documentation":"Raised by sylvester(number) in maths/sylvester_sequence.py when number is a builtin int less than 1. The function computes the n-th Sylvester sequence term recursively (sylvester(1) == 2, then a(n) = a(n-1)^2 - a(n-1) + 1), so 0 and negative indices are rejected. The type check above it uses a bare assert, so non-integers raise AssertionError instead — another quirk of this module.","triggerScenarios":"Calling sylvester(0) or sylvester(-3). Note sylvester(8.0) raises AssertionError (not this ValueError) because the assert fires first; also note values grow doubly-exponentially, so large n is slow and huge.","commonSituations":"0-based indexing into this 1-based sequence; running with python -O strips the assert, letting 8.0 slip through to a TypeError elsewhere — a subtle behavior change under optimized mode.","solutions":["Use indices >= 1: shift 0-based indices with sylvester(i + 1)","Do not rely on the assert for validation — it disappears under `python -O`; validate types yourself","Cap n for performance: terms explode in size (term 8 is already ~2^32 digits-scale growth per step)"],"exampleFix":"// before\nv = sylvester(0)  # ValueError\n\n// after\nv = sylvester(1)  # 2","handlingStrategy":"validation","validationCode":"if not isinstance(n, int) or isinstance(n, bool):\n    raise TypeError('sylvester index must be an int')  # own check: assert vanishes under -O\nif n < 1:\n    raise ValueError('sylvester index must be >= 1')\nv = sylvester(n)","typeGuard":null,"tryCatchPattern":"try:\n    v = sylvester(n)\nexcept (ValueError, AssertionError) as e:\n    raise ValueError(f'bad sylvester index {n!r}: {e}') from e","preventionTips":["1-based sequence: sylvester(1) == 2","The type check is a bare assert — it disappears under python -O, so never rely on it","Terms grow doubly-exponentially; cache or cap n for performance"],"tags":["math","valueerror","input-validation","recursion","assertion-quirk","sylvester"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}