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