Meteoric Stream - Original Apps & Music, Piano Instrumental, Children music, Hymns


Japanese | English
Meteoric Stream -> Documentation -> Python -> [Python] Calculate UPC code check digit

[Python] Calculate UPC code check digit

I wrote calculate function for UPC check digit with Python.
UPC code (UPC-A) has below 12 digits:

No.1 : Number System Character
No.2 - 6 : Company Code (5 digits)
No.7 - 11 : Item Code (5 digits)
No.12 : Check Digit

Last of digit is a check digit.

It has mathematical formula that an totals of even number of digits multiplied by 3 plus totals of odd digits, and division remainder of above-mentioned minus 10, and convert to a natural number.

...oops, it's difficult...

Actually, it's very easy when there is code.

def upc_check_digit(number): return str(10 - (( int(number[0]) + int(number[2]) + int(number[4]) + int(number[6]) + int(number[8]) + int(number[10]) ) * 3 + ( int(number[1]) + int(number[3]) + int(number[5]) + int(number[7]) + int(number[9]))) % 10) print(upc_check_digit("12345678901"))
Result: 2 (string) It's easy.

Lastupdated:2017/11/15
Wrote by:2017/11/09

Share this article

Related


Go To Documentation Top
Meteoric Stream -> Documentation -> Python -> [Python] Calculate UPC code check digit
Go to Top