forked from avinashkranjan/Amazing-Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathISBN-Number.py
64 lines (48 loc) · 1.63 KB
/
ISBN-Number.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Checking entered number is a isbn number or not
def valid_ISBN(isbn):
# Remove hyphens and spaces from the ISBN
isbn = isbn.replace('-', '').replace(' ', '')
# Check if the length of the ISBN is valid
if len(isbn) == 10:
return valid_ISBN10(isbn)
elif len(isbn) == 13:
return valid_ISBN13(isbn)
else:
return False
# Checking the entered number is a valid 10-digit isbn number
def valid_ISBN10(isbn):
# Check if the ISBN is valid according to the ISBN-10 algorithm
if not isbn[:-1].isdigit() or not isbn[-1] in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'X']:
return False
# Calculate the check digit
checkDigit = 0
weight = 10
for digit in isbn[:-1]:
checkDigit += int(digit) * weight
weight -= 1
checkDigit %= 11
if isbn[-1] == 'X':
checkDigit = 'X'
# Validate the check digit
return str(checkDigit) == isbn[-1]
# Checking the entered number is a valid 13-digit isbn number
def valid_ISBN13(isbn):
# Check if the ISBN is valid according to the ISBN-13 algorithm
if not isbn.isdigit():
return False
# Calculate the check digit
checkDigit = 0
weight = [1, 3] * 6
for digit, w in zip(isbn[:-1], weight):
checkDigit += int(digit) * w
checkDigit %= 10
checkDigit = (10 - checkDigit) % 10
# Validate the check digit
return str(checkDigit) == isbn[-1]
# Ask the user to enter an ISBN number
isbnNumber = input("\nEnter an ISBN number: ")
# Validate the ISBN number
if valid_ISBN(isbnNumber):
print("\nValid ISBN number.\n")
else:
print("\nInvalid ISBN number.\n")