{"record":{"id":"f4d59b992a1f985d","repo":"TheAlgorithms/Python","slug":"the-entered-barcode-has-a-negative-value-try-agai","errorCode":null,"errorMessage":"The entered barcode has a negative value. Try again.","messagePattern":"The entered barcode has a negative value\\. Try again\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"strings/barcode_validator.py","lineNumber":71,"sourceCode":"    return len(str(barcode)) == 13 and get_check_digit(barcode) == barcode % 10\n\n\ndef get_barcode(barcode: str) -> int:\n    \"\"\"\n    Returns the barcode as an integer\n\n    >>> get_barcode(\"8718452538119\")\n    8718452538119\n    >>> get_barcode(\"dwefgiweuf\")\n    Traceback (most recent call last):\n        ...\n    ValueError: Barcode 'dwefgiweuf' has alphabetic characters.\n    \"\"\"\n    if str(barcode).isalpha():\n        msg = f\"Barcode '{barcode}' has alphabetic characters.\"\n        raise ValueError(msg)\n    elif int(barcode) < 0:\n        raise ValueError(\"The entered barcode has a negative value. Try again.\")\n    else:\n        return int(barcode)\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n    \"\"\"\n    Enter a barcode.\n\n    \"\"\"\n    barcode = get_barcode(input(\"Barcode: \").strip())\n\n    if is_valid(barcode):\n        print(f\"'{barcode}' is a valid barcode.\")\n    else:\n        print(f\"'{barcode}' is NOT a valid barcode.\")","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/strings/barcode_validator.py#L53-L89","documentation":"Raised by get_barcode in strings/barcode_validator.py when the barcode parses to a negative integer (int(barcode) < 0). Barcodes are conventionally non-negative digit strings, and the checksum validation downstream assumes that, so values like '-123' (where int() succeeds but is negative) are rejected with ValueError. It only fires after the isalpha() check, i.e. the input parsed successfully as an int.","triggerScenarios":"get_barcode('-123'); get_barcode('−8718452538119' with a leading minus); programmatic input where a signed field is passed through. Note '12-3' or '1.2' would instead crash at int() with an uncaught ValueError, not this error.","commonSituations":"Numeric IDs stored as signed integers elsewhere in the pipeline; CSV columns with minus signs; users typing a dash before a digit sequence.","solutions":["Reject or correct negative values at your input boundary before calling get_barcode.","Strip leading '+'/'-' and validate digits-only: if not s.isdigit(): reject.","If a negative value indicates bad upstream data, log and re-prompt rather than silently taking abs()."],"exampleFix":"# before\nget_barcode(code)  # code = '-8718452538119'\n\n# after\nif not code.isdigit():\n    raise ValueError('Barcode must be a non-negative digit string')\nget_barcode(code)","handlingStrategy":"validation","validationCode":"code = str(barcode).strip()\nif not code.isdigit():  # rejects '-', '+', '.', and letters in one check\n    raise ValueError('Barcode must be a non-negative digit string')\nvalue = get_barcode(code)","typeGuard":null,"tryCatchPattern":"try:\n    value = get_barcode(code)\nexcept ValueError as e:\n    if 'negative' in str(e):\n        raise ValueError('Signed values are not valid barcodes') from e\n    raise","preventionTips":["Validate digits-only before calling; this also prevents the separate int() parse crash.","Do not abs() a negative barcode silently — it usually masks bad upstream data.","Keep ID columns unsigned where barcodes originate (DB schema, CSV parsing)."],"tags":["strings","barcode","validation","negative-value","user-input"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}