Validate ISBN in Python
def is_isbn(isbn: str) -> bool:
# ISBN-13 must be exactly 13 digits
if len(isbn) != 13 or not isbn.isdigit():
return False
# Calculate the weighted sum
total = 0
for i in range(len(isbn) - 1):
d = int(isbn[i])
total += d * (1 if i % 2 == 0 else 3)
# Calculate the check digit
check = (10 - total % 10) % 10
# Verify check digit and divisibility by 10
return check == int(isbn[12]) and (total + check) % 10 == 0
def main():
isbn = input("Enter an ISBN: ")
valid = is_isbn(isbn)
print(valid)
if __name__ == "__main__":
main()