Last active
January 12, 2022 16:20
-
-
Save emorisse/7fffb313f1459208b55be8dcc067ebe6 to your computer and use it in GitHub Desktop.
Add checksum to string based on ISBN method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
sku = "ABCDEFG" | |
def checksum(sku): | |
sum = 0 | |
for i,c in enumerate(sku): | |
o = ord(c) | |
#print("{},{}".format(c,o)) | |
if i % 2 == 1: | |
sum += o | |
else: | |
sum += o * 3 | |
letters = [x for x in "ABCDEFGHIJ"] | |
checksum = letters[sum%10] | |
return(checksum) | |
def verify(sku): | |
sku = [x for x in sku] | |
check = sku[len(sku)-1] | |
sku = sku[0:(len(sku)-1)] | |
if check == checksum(sku): | |
return(True) | |
return(False) | |
joint = sku + checksum(sku) | |
print(sku) | |
print(joint) | |
print(verify(joint)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Seems like a handy addition for hand entered SKU etc