TheAlgorithms/Python · error · ValueError
The entered barcode has a negative value. Try again.
Error message
The entered barcode has a negative value. Try again.
What it means
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.
Source
Thrown at strings/barcode_validator.py:71
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.")
else:
print(f"'{barcode}' is NOT a valid barcode.")View on GitHub (pinned to f5988cc097)
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().
Example fix
# before
get_barcode(code) # code = '-8718452538119'
# after
if not code.isdigit():
raise ValueError('Barcode must be a non-negative digit string')
get_barcode(code) Defensive patterns
Strategy: validation
Validate before calling
code = str(barcode).strip()
if not code.isdigit(): # rejects '-', '+', '.', and letters in one check
raise ValueError('Barcode must be a non-negative digit string')
value = get_barcode(code) Try / catch
try:
value = get_barcode(code)
except ValueError as e:
if 'negative' in str(e):
raise ValueError('Signed values are not valid barcodes') from e
raise Prevention
- 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).
When it happens
Trigger: 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.
Common situations: Numeric IDs stored as signed integers elsewhere in the pipeline; CSV columns with minus signs; users typing a dash before a digit sequence.
Related errors
- Barcode '{barcode}' has alphabetic characters.
- invalid input option
- Gravitational force can not be negative
- Distance can not be negative
- Mass can not be negative
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/f4d59b992a1f985d.
Report an issue: GitHub.