UPC Barcode & Check Digit Calculation

UPC-A-BarcodeA barcode is an optical, representation of data that can easily be scanned by a barcode reader.

A barcode is used to store a number such as the product code of a product for sale in a shop, or the ISBN number of a book. The barcode you scan in a shop most likely uses the UPC (Universal Product Code) format which means that it consists of 12 numerical digits (0 to 9).

With a UPC barcode, the last digit is called the check digit. The check digit is used to make sure a barcode has been entered (typed) or scanned correctly to minimize human errors or scanning errors.

The check digit is a result of a complex calculation based on the 11 first digits of the barcode. Every time a barcode is scanned, the computer completes this calculation again using the first 11 digits to calculate the check digit. it then compares this with the 12th digit of the barcode. If these two are the same, the barcode scanned is most likely valid. If they are diffrent the scanner will beep to indicate that the barcode was not scanned properly.

The check digit is a fairly complex calculation that is performed in 3 steps as follows:


Check Digit Calculation


Check Digit Validation

Your challenge consists of writing a Python program that will receive a 12-digit UPC barcode (11 digits + 1 check digit). Your Python script will then calculate the expected check digit matching the 11-digit product code. If this calculated check digit matches the 12th digit of the barcode, your program will output “Valid barcode”, if not it will output “Invalid barcode” on the screen.

Invalid Barcode?

You will then test your program with the following 6 barcodes. One of them is invalid! Use your program to find it:

# Barcode UPC-A Valid or Invalid?
#1 barcode-upc-a-1 123456789012
#2 barcode-upc-a-2 796483659834
#3 barcode-upc-a-7 572451780690
#4 barcode-not-upc-a-3 967483921543
#5 barcode-upc-a-4 485932587605
#6 barcode-upc-a-5 483025016933
#7 barcode-upca-a-6 659047763326
Share Button
Tagged with: