A 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:
- Add the digits in the odd-numbered positions (first, third, fifth, etc.) together and multiply by three.
- Add the digits (up to but not including the check digit) in the even-numbered positions (second, fourth, sixth, etc.) to the result.
- Take the remainder of the result divided by 10 (modulo operation) and if not 0, subtract this from 10 to derive the check digit.
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 | 123456789012 | ||
#2 | 796483659834 | ||
#3 | 572451780690 | ||
#4 | 967483921543 | ||
#5 | 485932587605 | ||
#6 | 483025016933 | ||
#7 | 659047763326 |
Solution...
The solution for this challenge is available to full members!Find out how to become a member:
➤ Members' Area
Solution...
You are viewing this solution as part of your full membership subscription!Python Code
#Check Digit Validation - www.101computing.net/upc-barcode-check-digit-calculation/ barcode = input("Enter 12-digit UPC-A barcode:") while len(barcode)!=12: print("Invalid UPC-A barcode.") barcode = input("Re-enter 12-digit UPC-A barcode:") checkDigit = int(barcode[11]) #12th digit print("Check digit: " + str(checkDigit)) #Calculate check digit oddDigits = int(barcode[0]) + int(barcode[2]) + int(barcode[4]) + int(barcode[6]) + int(barcode[8]) + int(barcode[10]) evenDigits = int(barcode[1]) + int(barcode[3]) + int(barcode[5]) + int(barcode[7]) + int(barcode[9]) total = oddDigits * 3 + evenDigits remainder = total % 10 if remainder == 0: calculatedCheckDigit = 0 else: calculatedCheckDigit = 10 - remainder print("Calculated Check Digit: " + str(calculatedCheckDigit)) #Compare both check digits to see if the barcode is valid if checkDigit == calculatedCheckDigit: print("Valid Barcode") else: print("Invalid Barcode")