View Single Post
Old 12-06-2022, 04:26 PM   #2
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,449
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Code:
def evaluate(self, formatter, kwargs, mi, locals):
    isbn = mi.isbn
    if len(isbn) != 10:
        return 'isbn length != 10'
    prefix = '978' + isbn[:-1]
    check = self.check_digit_13(prefix)
    return prefix + check

def check_digit_13(self, isbn):
    assert len(isbn) == 12
    sum = 0
    for i in range(len(isbn)):
        c = int(isbn[i])
        if i % 2: w = 3
        else: w = 1
        sum += w * c
    r = 10 - (sum % 10)
    if r == 10: return '0'
    else: return str(r)
Attached Thumbnails
Click image for larger version

Name:	Clipboard01.png
Views:	94
Size:	57.3 KB
ID:	198238  
chaley is offline   Reply With Quote