{"record":{"id":"eae0329f674546f9","repo":"TheAlgorithms/Python","slug":"barcode-barcode-has-alphabetic-characters","errorCode":null,"errorMessage":"Barcode '{barcode}' has alphabetic characters.","messagePattern":"Barcode '(.+?)' has alphabetic characters\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"strings/barcode_validator.py","lineNumber":69,"sourceCode":"    NameError: name 'dwefgiweuf' is not defined\n    \"\"\"\n    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.\")","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/strings/barcode_validator.py#L51-L87","documentation":"Raised by get_barcode in strings/barcode_validator.py when the barcode argument consists entirely of alphabetic characters (str(barcode).isalpha() is True). The helper converts the barcode to an int for checksum validation, and pure-letter input cannot be a barcode, so it is rejected with ValueError. Caveat: the isalpha() guard is narrow — mixed alphanumeric input like 'ab12' or '12ab' passes it and then crashes at int(barcode) with a different, uncaught ValueError from Python itself.","triggerScenarios":"get_barcode('dwefgiweuf'); get_barcode('abc'). Passing 'ab123' does NOT hit this error — it fails later at int('ab123') with ValueError: invalid literal for int().","commonSituations":"Form fields where users type a product name into the barcode box; OCR output that read letters; test fixtures with placeholder strings.","solutions":["Validate with str.isdigit() before calling: if not barcode.isdigit(): reject. This also covers the mixed-alphanumeric gap.","Normalize input: strip whitespace and reject non-digit characters at the boundary of your application.","If you hit this from user input, prompt again instead of catching and continuing with a bad value."],"exampleFix":"# before\nget_barcode(user_input)  # user typed 'dwefgiweuf'\n\n# after\nif not user_input.strip().isdigit():\n    raise ValueError('Barcode must contain digits only')\nget_barcode(user_input.strip())","handlingStrategy":"validation","validationCode":"code = str(barcode).strip()\nif not code.isdigit():\n    raise ValueError('Barcode must contain digits only')\nvalue = get_barcode(code)","typeGuard":"def is_digit_string(s: str) -> bool:\n    return isinstance(s, str) and s.isdigit()","tryCatchPattern":"try:\n    value = get_barcode(user_input.strip())\nexcept ValueError as e:\n    # covers alphabetic, negative, and int() parse failures\n    show_error_to_user(str(e))\n    value = ask_for_barcode_again()","preventionTips":["isalpha() only catches pure-letter input; use isdigit() to also catch mixed strings.","Strip whitespace before validating barcode form.","For interactive use, re-prompt on ValueError rather than swallowing it."],"tags":["strings","barcode","validation","user-input"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}