{"record":{"id":"06591d095feab076","repo":"TheAlgorithms/Python","slug":"num-invalid-input-please-enter-a-positive-inte","errorCode":null,"errorMessage":"{num}: Invalid input, please enter a positive integer.","messagePattern":"(.+?): Invalid input, please enter a positive integer\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/sieve_of_eratosthenes.py","lineNumber":39,"sourceCode":"    Returns a list with all prime numbers up to n.\r\n\r\n    >>> prime_sieve(50)\r\n    [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]\r\n    >>> prime_sieve(25)\r\n    [2, 3, 5, 7, 11, 13, 17, 19, 23]\r\n    >>> prime_sieve(10)\r\n    [2, 3, 5, 7]\r\n    >>> prime_sieve(9)\r\n    [2, 3, 5, 7]\r\n    >>> prime_sieve(2)\r\n    [2]\r\n    >>> prime_sieve(1)\r\n    []\r\n    \"\"\"\r\n\r\n    if num <= 0:\r\n        msg = f\"{num}: Invalid input, please enter a positive integer.\"\r\n        raise ValueError(msg)\r\n\r\n    sieve = [True] * (num + 1)\r\n    prime = []\r\n    start = 2\r\n    end = int(math.sqrt(num))\r\n\r\n    while start <= end:\r\n        # If start is a prime\r\n        if sieve[start] is True:\r\n            prime.append(start)\r\n\r\n            # Set multiples of start be False\r\n            for i in range(start * start, num + 1, start):\r\n                if sieve[i] is True:\r\n                    sieve[i] = False\r\n\r\n        start += 1\r\n\r","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/sieve_of_eratosthenes.py#L21-L57","documentation":"Raised by prime_sieve() in maths/sieve_of_eratosthenes.py when num <= 0. The sieve allocates [True] * (num + 1) and marks composites from 2 upward; zero and negative numbers have no primes below them, so the input is rejected up front. num=1 is valid and returns [] (empty prime list).","triggerScenarios":"Calling prime_sieve(0) or prime_sieve(-5). Note the message interpolates the offending value, e.g. '-5: Invalid input, please enter a positive integer.'. Non-integer input is not checked here and may fail elsewhere (e.g. float triggers a TypeError at list multiplication).","commonSituations":"Boundary code where the upper limit comes from a subtraction (limit - offset) that can go negative; off-by-one loops passing 0; parsing optional CLI args whose default is 0.","solutions":["Clamp to the smallest meaningful bound: prime_sieve(max(2, num))","Validate the bound at the source: if num < 1: raise/skip early in your own code","Treat num <= 1 as 'no primes' and return [] yourself before calling"],"exampleFix":"# before\nprimes = prime_sieve(upper - lower)  # can be <= 0\n\n# after\nspan = upper - lower\nprimes = prime_sieve(span) if span >= 1 else []","handlingStrategy":"validation","validationCode":"primes = prime_sieve(num) if num >= 1 else []","typeGuard":"def is_positive_int(value: object) -> bool:\n    return isinstance(value, int) and value > 0","tryCatchPattern":null,"preventionTips":["Clamp bounds: max(2, num) when you always want primes","Check subtraction results that produce the bound","num=1 is valid ([] result); only <= 0 raises"],"tags":["python","math","primes","validation","value-error"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}