{"record":{"id":"4e7e53bb7e961e2e","repo":"TheAlgorithms/Python","slug":"number-must-be-a-positive-integer-4e7e53","errorCode":null,"errorMessage":"number must be a positive integer","messagePattern":"number must be a positive integer","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/special_numbers/harshad_numbers.py","lineNumber":44,"sourceCode":"    ValueError: 'base' must be between 2 and 36 inclusive\n    >>> int_to_base(98, 37)\n    Traceback (most recent call last):\n        ...\n    ValueError: 'base' must be between 2 and 36 inclusive\n    >>> int_to_base(-99, 16)\n    Traceback (most recent call last):\n        ...\n    ValueError: number must be a positive integer\n    \"\"\"\n\n    if base < 2 or base > 36:\n        raise ValueError(\"'base' must be between 2 and 36 inclusive\")\n\n    digits = \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\"\n    result = \"\"\n\n    if number < 0:\n        raise ValueError(\"number must be a positive integer\")\n\n    while number > 0:\n        number, remainder = divmod(number, base)\n        result = digits[remainder] + result\n\n    if result == \"\":\n        result = \"0\"\n\n    return result\n\n\ndef sum_of_digits(num: int, base: int) -> str:\n    \"\"\"\n    Calculate the sum of digit values in a positive integer\n    converted to the given 'base'.\n    Where 'base' ranges from 2 to 36.\n\n    Examples:","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/special_numbers/harshad_numbers.py#L26-L62","documentation":"Raised by int_to_base() when number is negative. The algorithm repeatedly divmods a non-negative integer to extract digits and has no path for a minus sign, so negative inputs are rejected up front. Note the message says 'positive' but 0 is actually accepted and returns '0'.","triggerScenarios":"Calling int_to_base(number, base) with any number < 0, e.g. int_to_base(-99, 16). Base is validated first, so a bad base on a negative number raises the base error instead.","commonSituations":"Feeding unchecked signed arithmetic results or parsed negative user input into the converter; forgetting that this API, unlike Python's built-in hex()/oct(), has no negative-number support.","solutions":["Take abs(number) before converting if you only need the magnitude's digits","Reject or handle negative inputs at your own call site before passing them down","If a signed representation is required, prefix '-' yourself after converting the absolute value"],"exampleFix":"// before\ns = int_to_base(-99, 16)  # ValueError\n\n// after\ns = ('-' if n < 0 else '') + int_to_base(abs(-99), 16)","handlingStrategy":"validation","validationCode":"def to_base_signed(n: int, base: int) -> str:\n    sign = '-' if n < 0 else ''\n    return sign + int_to_base(abs(n), base)","typeGuard":null,"tryCatchPattern":"try:\n    s = int_to_base(n, base)\nexcept ValueError as e:\n    if 'positive' in str(e):\n        s = '-' + int_to_base(-n, base)\n    else:\n        raise","preventionTips":["Check n >= 0 before converting","Remember 0 is allowed and returns '0' despite the message wording","Centralize signed-conversion policy in one wrapper instead of ad-hoc abs() calls"],"tags":["math","input-validation","valueerror","negative-numbers"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}