Posts: 5 Threads: 2 Joined: Sep 2019 This is a code i made that checks whether an isbn with 13 digits is valid, but there is something wrong but I don't know what it is. The principle is like this for example, 978-0-306-40615-? is calculated as follows: sum = 9×1 + 7×3 + 8×1 + 0×3 + 3×1 + 0×3 + 6×1 + 4×3 + 0×1 + 6×3 + 1×1 + 5×3 = 9 + 21 + 8 + 0 + 3 + 0 + 6 + 12 + 0 + 18 + 1 + 15 = 93 93 / 10 = 9 remainder 3 10 – 3 = 7 Thus, the check digit is 7, and the complete sequence is ISBN 978-0-306-40615-7. if the sum is divisble by 10, the remainder will be zero and 10-0=10, however, the check digit is still 0. However in the code, when I check 9781491939369, which is a valid isbn, it outputs invalid. isbn=input('ISBN: ') sum=(1*int(isbn[0])+3*int(isbn[1])+1*int(isbn[2])+3*int(isbn[3])+1*int(isbn[4])+3*int(isbn[5])+1*int(isbn[6])+3*int(isbn[7])+1*int(isbn[8])+3*int(isbn[9])+1*int(isbn[10])+3*int(isbn[11])) if sum%10==0: if isbn[12]==0: print('Valid') else: print('Invalid') else: if isbn[12]==10-(sum%10): print('Valid') else: print('Invalid')Thanks a lot. Posts: 4,874 Threads: 78 Joined: Jan 2018 Sep-28-2019, 09:38 AM (This post was last modified: Sep-28-2019, 09:39 AM by Gribouillis.) You need to convert isbn[12] to int as well. if int(isbn[12]) == 0: ... if int(isbn[12]) == ...: ... Posts: 8,197 Threads: 162 Joined: Sep 2016 Never use built-in functions, modules, packages as variable names. In this case you override built-in function sum() Posts: 5 Threads: 2 Joined: Sep 2019 Sep-28-2019, 11:01 AM (This post was last modified: Sep-28-2019, 11:20 AM by kumaaaa.) (Sep-28-2019, 09:38 AM)Gribouillis Wrote: You need to convert isbn[12] to int as well. if int(isbn[12]) == 0: ... if int(isbn[12]) == ...: ... Thanks for your reminder. (Sep-28-2019, 09:14 AM)kumaaaa Wrote: This is a code i made that checks whether an isbn with 13 digits is valid, but there is something wrong but I don't know what it is. The principle is like this for example, 978-0-306-40615-? is calculated as follows: sum = 9×1 + 7×3 + 8×1 + 0×3 + 3×1 + 0×3 + 6×1 + 4×3 + 0×1 + 6×3 + 1×1 + 5×3 = 9 + 21 + 8 + 0 + 3 + 0 + 6 + 12 + 0 + 18 + 1 + 15 = 93 93 / 10 = 9 remainder 3 10 – 3 = 7 Thus, the check digit is 7, and the complete sequence is ISBN 978-0-306-40615-7. if the sum is divisble by 10, the remainder will be zero and 10-0=10, however, the check digit is still 0. However in the code, when I check 9781491939369, which is a valid isbn, it outputs invalid. isbn=input('ISBN: ') sum=(1*int(isbn[0])+3*int(isbn[1])+1*int(isbn[2])+3*int(isbn[3])+1*int(isbn[4])+3*int(isbn[5])+1*int(isbn[6])+3*int(isbn[7])+1*int(isbn[8])+3*int(isbn[9])+1*int(isbn[10])+3*int(isbn[11])) if sum%10==0: if isbn[12]==0: print('Valid') else: print('Invalid') else: if isbn[12]==10-(sum%10): print('Valid') else: print('Invalid') Thanks a lot. Thank you very much. Posts: 1,953 Threads: 8 Joined: Jun 2018 As buran pointed out - never use sum as variable name. I would add - never use Python as typing machine. You should see the pattern in ISBN check number calculation: every second number in ISBN is multiplied with 3. So no need to do all this manual labor: >>> s = '978-0-306-40615' >>> nums = (int(num) for chunk in s.split('-') for num in chunk) # remove '-' and convert into int >>> calculated = (v if i % 2 == 0 else v * 3 for i, v in enumerate(nums)) # keep in mind that Python is 0-indexed >>> last = 10 - sum(calculated) % 10 >>> last 7 I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame. Posts: 5 Threads: 2 Joined: Sep 2019 (Sep-28-2019, 11:01 AM)kumaaaa Wrote: (Sep-28-2019, 09:38 AM)Gribouillis Wrote: You need to convert isbn[12] to int as well. if int(isbn[12]) == 0: ... if int(isbn[12]) == ...: ... Thanks for your reminder. (Sep-28-2019, 09:14 AM)kumaaaa Wrote: This is a code i made that checks whether an isbn with 13 digits is valid, but there is something wrong but I don't know what it is. The principle is like this for example, 978-0-306-40615-? is calculated as follows: sum = 9×1 + 7×3 + 8×1 + 0×3 + 3×1 + 0×3 + 6×1 + 4×3 + 0×1 + 6×3 + 1×1 + 5×3 = 9 + 21 + 8 + 0 + 3 + 0 + 6 + 12 + 0 + 18 + 1 + 15 = 93 93 / 10 = 9 remainder 3 10 – 3 = 7 Thus, the check digit is 7, and the complete sequence is ISBN 978-0-306-40615-7. if the sum is divisble by 10, the remainder will be zero and 10-0=10, however, the check digit is still 0. However in the code, when I check 9781491939369, which is a valid isbn, it outputs invalid. isbn=input('ISBN: ') sum=(1*int(isbn[0])+3*int(isbn[1])+1*int(isbn[2])+3*int(isbn[3])+1*int(isbn[4])+3*int(isbn[5])+1*int(isbn[6])+3*int(isbn[7])+1*int(isbn[8])+3*int(isbn[9])+1*int(isbn[10])+3*int(isbn[11])) if sum%10==0: if isbn[12]==0: print('Valid') else: print('Invalid') else: if isbn[12]==10-(sum%10): print('Valid') else: print('Invalid') Thanks a lot. Thank you very much. Thank you Posts: 2,171 Threads: 12 Joined: May 2017 Sep-28-2019, 12:06 PM (This post was last modified: Sep-28-2019, 12:06 PM by DeaD_EyE.) from itertools import cycle def strip_non_digit(text): """ Returns an empty string or a string with digits """ return ''.join(c for c in text if c.isdigit()) def ean_checksum(ean): """ bit unclear.. https://en.wikipedia.org/wiki/International_Article_Number#Check_digit The ean checksum is calculated as follows: 1. Multiply each digit with 1, then with 3, then with 1 ... 2. Make the sum 3. Calculate the rest of division by 10 (result % 10) """ ean = strip_non_digit(ean) factor = cycle((1,3)) return sum(int(digit) * factor for factor, digit in zip(factor, ean)) % 10 def isbn13_checksum(isbn): """ https://en.wikipedia.org/wiki/International_Standard_Book_Number#ISBN-13_check_digit_calculation The isbn checksum is calculated as follows: 1. Multiply each digit with 1, 2, 3, 4, ... 2. Make the sum 3. Calculate the rest of division by 11 (result % 11) """ isbn = strip_non_digit(isbn) index_digit = enumerate(isbn, start=1) # index, digit return sum(int(digit) * factor for factor, digit in index_digit) % 11 I am not complete sure about the names. The ean_checksum uses the factors 1, 3 instead of 3, 1. Here the german source, which I've used: https://www.arndt-bruenner.de/mathe/scri...iffern.htm |