Skip to content

Instantly share code, notes, and snippets.

@emorisse
Last active January 12, 2022 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emorisse/7fffb313f1459208b55be8dcc067ebe6 to your computer and use it in GitHub Desktop.
Save emorisse/7fffb313f1459208b55be8dcc067ebe6 to your computer and use it in GitHub Desktop.
Add checksum to string based on ISBN method
#!/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))
@emorisse
Copy link
Author

Seems like a handy addition for hand entered SKU etc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment