123k views
1 vote
Write a program that accepts an ISBN number and displays whether it is valid or not. An ISBN (International Standard Book Number) is a ten digit code which uniquely identifies a book. The first nine digits represent Group, Publisher and Title of the book and the last digit is used as a checksum (to check whether the given ISBN is valid). Each of the digits can take any value between 0 and 9. In addition, the last digit may also take a value X (stands for 10). To verify an ISBN, calculate 10 times the first digit, plus 9 times the second digit, plus 8 times the third and so on until we add 1 time the last digit. If the number 11 can divide the sum (no remainder), the code is a valid ISBN.

User Ubik
by
5.0k points

1 Answer

6 votes

Answer:

is_isbn = True

total = 0

number = input("Enter the ISBN: ")

if len(number) != 10:

is_isbn = False

for i in range(9):

if 0 <= int(number[i]) <= 9:

total += int(number[i]) * (10 - i)

else:

is_isbn = False

if number[9] != 'X' and int(number[9]) > 9:

is_isbn = False

if number[9] == 'X':

total += 10

else:

total += int(number[9])

if total % 11 != 0:

is_isbn = False

if is_isbn:

print("It is a valid ISBN")

else:

print("It is NOT a valid ISBN")

Step-by-step explanation:

Initialize the variables, total and is_isbn - keeps the value for the number if it is valid or not

Ask the user for the ISBN number

If number's length is less than 10, it is not valid, set is_isbn to false

Then, make calculation to verify the ISBN

Check the last digit, if it is not "X" and greater than 10, it is not valid, set is_isbn to false

Again, check the last digit, if it is "X", add 10 to the total, otherwise add its value

Check if total can divide 11 with no remainder. If not, it is not valid, set is_isbn to false

Finally, depending on the is_isbn variable, print the result

User Isaac Gonzalez
by
5.4k points