RosettaCodeData/Task/ISBN13-check-digit/Python/isbn13-check-digit-1.py
2024-03-06 22:25:12 -08:00

16 lines
427 B
Python

def is_isbn13(n):
n = n.replace('-','').replace(' ', '')
if len(n) != 13:
return False
product = (sum(int(ch) for ch in n[::2])
+ sum(int(ch) * 3 for ch in n[1::2]))
return product % 10 == 0
if __name__ == '__main__':
tests = '''
978-0596528126
978-0596528120
978-1788399081
978-1788399083'''.strip().split()
for t in tests:
print(f"ISBN13 {t} validates {is_isbn13(t)}")