Amazing-Python-Scripts

Форк
0
64 строки · 1.6 Кб
1
# Checking entered number is a isbn number or not
2
def valid_ISBN(isbn):
3
    # Remove hyphens and spaces from the ISBN
4
    isbn = isbn.replace('-', '').replace(' ', '')
5

6
    # Check if the length of the ISBN is valid
7
    if len(isbn) == 10:
8
        return valid_ISBN10(isbn)
9
    elif len(isbn) == 13:
10
        return valid_ISBN13(isbn)
11
    else:
12
        return False
13

14
# Checking the entered number is a valid 10-digit isbn number
15

16

17
def valid_ISBN10(isbn):
18
    # Check if the ISBN is valid according to the ISBN-10 algorithm
19
    if not isbn[:-1].isdigit() or not isbn[-1] in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'X']:
20
        return False
21

22
    # Calculate the check digit
23
    checkDigit = 0
24
    weight = 10
25
    for digit in isbn[:-1]:
26
        checkDigit += int(digit) * weight
27
        weight -= 1
28

29
    checkDigit %= 11
30
    if isbn[-1] == 'X':
31
        checkDigit = 'X'
32

33
    # Validate the check digit
34
    return str(checkDigit) == isbn[-1]
35

36
# Checking the entered number is a valid 13-digit isbn number
37

38

39
def valid_ISBN13(isbn):
40
    # Check if the ISBN is valid according to the ISBN-13 algorithm
41
    if not isbn.isdigit():
42
        return False
43

44
    # Calculate the check digit
45
    checkDigit = 0
46
    weight = [1, 3] * 6
47
    for digit, w in zip(isbn[:-1], weight):
48
        checkDigit += int(digit) * w
49

50
    checkDigit %= 10
51
    checkDigit = (10 - checkDigit) % 10
52

53
    # Validate the check digit
54
    return str(checkDigit) == isbn[-1]
55

56

57
# Ask the user to enter an ISBN number
58
isbnNumber = input("\nEnter an ISBN number: ")
59

60
# Validate the ISBN number
61
if valid_ISBN(isbnNumber):
62
    print("\nValid ISBN number.\n")
63
else:
64
    print("\nInvalid ISBN number.\n")
65

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.