-5

0000000000000 is a valid ISBN number, but my code says it's invalid. My code takes a 13-digit number and checks if it's valid or not.

Each of the first 12 digits of the ISBN is alternately multiplied by 1 and 3. Then, we sum them up and divide the sum by 10, and get the reminder. If 10 minus the reminder is equal to the 13th digit, then it's valid.

isbnNunmberCheck=input()

n1=int(isbnNunmberCheck[0])*1
n2=int(isbnNunmberCheck[1])*3
n3=int(isbnNunmberCheck[2])*1
n4=int(isbnNunmberCheck[3])*3
n5=int(isbnNunmberCheck[4])*1
n6=int(isbnNunmberCheck[5])*3
n7=int(isbnNunmberCheck[6])*1
n8=int(isbnNunmberCheck[7])*3
n9=int(isbnNunmberCheck[8])*1
n10=int(isbnNunmberCheck[9])*3
n11=int(isbnNunmberCheck[10])*1
n12=int(isbnNunmberCheck[11])*3

k=10-(n1+n2+n3+n4+n5+n6+n7+n8+n9+n10+n11+n12)%10

if k==int(isbnNunmberCheck[-1]):
    print("Valid")
else:
    print("Invalid")
Yolanda Hui
  • 97
  • 1
  • 1
  • 8
  • 1
    10 - 0 != 0... are you sure its valid? – Sayse Oct 15 '18 at 13:11
  • 2
    `0 % 10` is `0`, `10 - 0` is `10`, `10` is not `0`. – DeepSpace Oct 15 '18 at 13:12
  • 4
    if you have 100 numbers, will you create 100 variables? – Jean-François Fabre Oct 15 '18 at 13:13
  • 0000000000000 is a valid ISBN number, its the only one exception. According to the ISBN Users' Manual, “Each of the first 12 digits of the ISBN is alternately multiplied by 1 and 3. The check digit is equal to 10 minus the remainder resulting from dividing the sum of the weighted products of the first 12 digits by 10 with one exception. If this calculation results in an apparent check digit of 10, the check digit is 0.” – Yolanda Hui Oct 15 '18 at 13:13
  • 4
    If it's an exception and you haven't handled the exception, why did you think it would work? – r.ook Oct 15 '18 at 13:14
  • if isbn==0000000000000: print("Valid") is not working – Yolanda Hui Oct 15 '18 at 13:15
  • it's not the only exception. Remainder 0 means that 10- = 10, which cannot be equal to any digit. – alexis Oct 15 '18 at 13:15
  • Possible duplicate: [Checking if an ISBN number is correct](https://stackoverflow.com/q/4047511/1324033) – Sayse Oct 15 '18 at 13:16
  • @DeepSpace: The parenthesis did make a difference. At least the number is now valid ;) The OP must look more into it – Sheldore Oct 15 '18 at 13:16
  • @Jean-FrançoisFabre how do I simplify it with a for loop? for i range (1,12): – Yolanda Hui Oct 15 '18 at 14:06

3 Answers3

1

Well, your sum is 0, so the remainder will be 0, then (10 - the remainder) = 10, so 10 != 0.

I think looking at this link, your alogrithm is incorrect to begin with.

0

If 10 minus the remainder is equal to the 13th digit, then it's valid.

For your ISBN, "10 minus the remainder" is 10 - 0 % 10 = 10. This is not equal to the 13th digit, which is 0. Since 10 != 0, the ISBN is invalid.

But your code is verbose. Let's consider a better way of writing your algorithm:

isbn_str = input()

k = 10 - (sum(map(int, isbn_str[0:-1:2])) + sum(map(int, isbn_str[1:-1:2]))*3) % 10

print(k)

if k == int(isbn_str[-1]):
    print("Valid")
else:
    print("Invalid")

Notice you can slice strings in the same way you can slice lists, i.e. with the syntax start:stop:step. map + int converts each element of an iterable to an integer lazily and sum takes those integers and sums them. See also Understanding Python's slice notation.

jpp
  • 159,742
  • 34
  • 281
  • 339
  • Care to explain your "better way"? – Sheldore Oct 15 '18 at 13:16
  • @Bazingaa, Yes, see my explanation below the code. – jpp Oct 15 '18 at 13:16
  • That still outputs `0000000000000` as `Invalid` – DeepSpace Oct 15 '18 at 13:17
  • @DeepSpace, Yes, because it is :) – jpp Oct 15 '18 at 13:18
  • 1
    Minor grievance, but it's ISBN not ISIN :). Note given the original piece of code I doubt OP has all the fundamentals to understand what's going on here though. – r.ook Oct 15 '18 at 13:19
  • @jpg It is **valid**, according to wikipedia: https://en.wikipedia.org/wiki/International_Standard_Book_Number#ISBN-13_check_digit_calculation If `r` (`k` in this case) is 10 then the check digit (the 13th) should be `0`. – DeepSpace Oct 15 '18 at 13:19
  • @DeepSpace, But not according to OP's algorithm. I can't (or am not willing to) fit their logic with reality, but I'm happy to improve existing logic. – jpp Oct 15 '18 at 13:20
  • @jpp Fair enough, but I think your answer should at least point out that OP's algorithm is wrong – DeepSpace Oct 15 '18 at 13:21
  • @DeepSpace, *But it's right*... according to their logic "Each of the first 12 digits of the ISBN is alternately multiplied by 1 and 3... If 10 minus the reminder is equal to the 13th digit, then it's valid." I see what you are saying. But I don't know the ISBN rules and IMO are outside SO scope *unless defined in the question*. – jpp Oct 15 '18 at 13:22
0

The key statement is:

If this calculation results in an apparent check digit of 10, the check digit is 0

from your comment NOT your original question.

So if the check digit comes out to be 10 you actually need to use 0 in your comparison.

Calculate your check number then:

k = 0 if k == 10 else k
T Burgis
  • 1,395
  • 7
  • 9