TheAlgorithms/Python · error · ValueError
Barcode '{barcode}' has alphabetic characters.
Error message
Barcode '{barcode}' has alphabetic characters. What it means
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.
Source
Thrown at strings/barcode_validator.py:69
NameError: name 'dwefgiweuf' is not defined
"""
return len(str(barcode)) == 13 and get_check_digit(barcode) == barcode % 10
def get_barcode(barcode: str) -> int:
"""
Returns the barcode as an integer
>>> get_barcode("8718452538119")
8718452538119
>>> get_barcode("dwefgiweuf")
Traceback (most recent call last):
...
ValueError: Barcode 'dwefgiweuf' has alphabetic characters.
"""
if str(barcode).isalpha():
msg = f"Barcode '{barcode}' has alphabetic characters."
raise ValueError(msg)
elif int(barcode) < 0:
raise ValueError("The entered barcode has a negative value. Try again.")
else:
return int(barcode)
if __name__ == "__main__":
import doctest
doctest.testmod()
"""
Enter a barcode.
"""
barcode = get_barcode(input("Barcode: ").strip())
if is_valid(barcode):
print(f"'{barcode}' is a valid barcode.")View on GitHub (pinned to f5988cc097)
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.
Example fix
# before
get_barcode(user_input) # user typed 'dwefgiweuf'
# after
if not user_input.strip().isdigit():
raise ValueError('Barcode must contain digits only')
get_barcode(user_input.strip()) Defensive patterns
Strategy: validation
Validate before calling
code = str(barcode).strip()
if not code.isdigit():
raise ValueError('Barcode must contain digits only')
value = get_barcode(code) Type guard
def is_digit_string(s: str) -> bool:
return isinstance(s, str) and s.isdigit() Try / catch
try:
value = get_barcode(user_input.strip())
except ValueError as e:
# covers alphabetic, negative, and int() parse failures
show_error_to_user(str(e))
value = ask_for_barcode_again() Prevention
- 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.
When it happens
Trigger: 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().
Common situations: Form fields where users type a product name into the barcode box; OCR output that read letters; test fixtures with placeholder strings.
Related errors
- The entered barcode has a negative value. Try again.
- invalid input option
- Invalid Strand
- String lengths must match!
- Input value must be a positive integer
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/eae0329f674546f9.
Report an issue: GitHub.