Untitled

mail@pastecode.io avatar
unknown
python
a month ago
426 B
1
Indexable
Never
def is_valid(isbn):
    isbn = isbn.replace("-", "")

    if len(isbn) != 10 or (isbn[-1] != "X" and not isbn[-1].isnumeric()):
        return False

    check = isbn[-1]

    if check == "X":
        check = 10

    sum = 0

    for i, digit in enumerate(isbn[:-1]):
        if not digit.isnumeric():
            return False

        sum += int(digit) * (10 - i)

    return True if ((sum + int(check)) % 11) == 0 else False
Leave a Comment