Skip to content

Commit 65ba1b5

Browse files
committed
Fix erroneous calculation in calculate_checksum
See: #213
2 parents c5fac0a + da2c542 commit 65ba1b5

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

barcode/ean.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,10 @@ def calculate_checksum(self) -> int:
8686
def sum_(x, y):
8787
return int(x) + int(y)
8888

89-
evensum = reduce(sum_, self.ean[-2::-2])
90-
oddsum = reduce(sum_, self.ean[-1::-2])
89+
ean_without_checksum = self.ean[: self.digits]
90+
91+
evensum = reduce(sum_, ean_without_checksum[-2::-2])
92+
oddsum = reduce(sum_, ean_without_checksum[-1::-2])
9193
return (10 - ((evensum + oddsum * 3) % 10)) % 10
9294

9395
def build(self):

tests/test_ean.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from __future__ import annotations
2+
3+
from barcode.ean import EAN13
4+
5+
6+
def test_ean_checksum() -> None:
7+
ean = EAN13("842169142322") # input has 12 digits
8+
assert ean.calculate_checksum() == 0
9+
10+
ean = EAN13("8421691423220") # input has 13 digits
11+
assert ean.calculate_checksum() == 0

0 commit comments

Comments
 (0)