Skip to content

Latest commit

 

History

History
20 lines (18 loc) · 555 Bytes

barcode_python_ean13.md

File metadata and controls

20 lines (18 loc) · 555 Bytes

Como generar en Python un código de Barra

def calculate_checksum(ean):
    """
    Calculates the checksum for an EAN13
    @param list ean: List of 12 numbers for first part of EAN13
    :returns: The checksum for `ean`.
    :rtype: Integer
    """
    assert len(ean) == 12, "EAN must be a list of 12 numbers"
    sum_ = lambda x, y: int(x) + int(y)
    evensum = reduce(sum_, ean[::2])
    oddsum = reduce(sum_, ean[1::2])
    return (10 - ((evensum + oddsum * 3) % 10)) % 10

a = '123456789012'
a = a + str(calculate_checksum(a))

print(a)