{"record":{"id":"ea970cd9bd702e8b","repo":"TheAlgorithms/Python","slug":"parameter-must-be-positive-int","errorCode":null,"errorMessage":"parameter must be positive int","messagePattern":"parameter must be positive int","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"conversions/decimal_to_any.py","lineNumber":63,"sourceCode":"    >>> decimal_to_any(10, '16') # doctest: +ELLIPSIS\n    Traceback (most recent call last):\n        ...\n    TypeError: 'str' object cannot be interpreted as an integer\n    >>> # a base less than 2 will error\n    >>> decimal_to_any(7, 0) # doctest: +ELLIPSIS\n    Traceback (most recent call last):\n        ...\n    ValueError: base must be >= 2\n    >>> # a base greater than 36 will error\n    >>> decimal_to_any(34, 37) # doctest: +ELLIPSIS\n    Traceback (most recent call last):\n        ...\n    ValueError: base must be <= 36\n    \"\"\"\n    if isinstance(num, float):\n        raise TypeError(\"int() can't convert non-string with explicit base\")\n    if num < 0:\n        raise ValueError(\"parameter must be positive int\")\n    if isinstance(base, str):\n        raise TypeError(\"'str' object cannot be interpreted as an integer\")\n    if isinstance(base, float):\n        raise TypeError(\"'float' object cannot be interpreted as an integer\")\n    if base in (0, 1):\n        raise ValueError(\"base must be >= 2\")\n    if base > 36:\n        raise ValueError(\"base must be <= 36\")\n    new_value = \"\"\n    mod = 0\n    div = 0\n    while div != 1:\n        div, mod = divmod(num, base)\n        if base >= 11 and 9 < mod < 36:\n            actual_value = ALPHABET_VALUES[str(mod)]\n        else:\n            actual_value = str(mod)\n        new_value += actual_value","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/decimal_to_any.py#L45-L81","documentation":"decimal_to_any raises ValueError('parameter must be positive int') when num < 0. The repeated-division algorithm (divmod until the quotient reaches 1) assumes a positive dividend; negatives would loop incorrectly. Note the message says 'positive' but 0 is accepted, and the float TypeError check runs before this, so only negative ints land here.","triggerScenarios":"decimal_to_any(-7, 2), decimal_to_any(-255, 16); computing deltas/offsets that can go negative and passing them unclamped.","commonSituations":"Temperature/difference conversions; arithmetic on unsigned-vs-signed mixes; tests that sweep negative values through every conversion helper.","solutions":["Handle the sign yourself: convert abs(num) and prefix '-' (or two's-complement for fixed width)","Clamp at the source if negatives are invalid in your domain: max(0, num)","Catch this ValueError alongside the TypeError/ValueError base checks when user input flows through"],"exampleFix":"# before\ndecimal_to_any(-10, 2)\n# ValueError: parameter must be positive int\n\n# after\nsign = '-' if n < 0 else ''\nsign + decimal_to_any(abs(-10), 2)  # '-1010'","handlingStrategy":"validation","validationCode":"if num < 0:\n    sign, num = '-', -num\nelse:\n    sign = ''\nresult = sign + decimal_to_any(num, base)","typeGuard":"def is_non_negative_int(v) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool) and v >= 0","tryCatchPattern":"try:\n    decimal_to_any(num, base)\nexcept ValueError as e:\n    if 'positive int' in str(e):\n        return '-' + decimal_to_any(-num, base)\n    raise","preventionTips":["Move sign handling to the caller","Clamp deltas with max(0, x) when negatives are impossible","Remember 0 is accepted; only negatives are rejected"],"tags":["conversions","validation","negative-numbers","base-conversion"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}