Python Forum
What's wrong with this code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What's wrong with this code?
#1
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.
Reply
#2
You need to convert isbn[12] to int as well.
    if int(isbn[12]) == 0:
    ...
    if int(isbn[12]) == ...:
    ...
Reply
#3
Never use built-in functions, modules, packages as variable names. In this case you override built-in function sum()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(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.
Reply
#5
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.
Reply
#6
(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
Reply
#7
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
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 484 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Something wrong with my code FabianPruitt 5 856 Jul-03-2023, 10:55 PM
Last Post: Pedroski55
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,561 Mar-27-2023, 07:38 AM
Last Post: buran
  Video recording with Raspberry Pi - What´s wrong with my python code? Montezuma1502 3 1,260 Feb-24-2023, 06:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,792 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,559 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Wrong code in Python exercise MaartenRo 2 1,531 Jan-01-2022, 04:12 PM
Last Post: MaartenRo
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,634 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  VS Code debugger using wrong Python environment topfox 0 2,511 Jun-09-2021, 10:01 AM
Last Post: topfox
  What is wrong with my code??? MrLeads 15 5,062 Sep-16-2020, 02:00 PM
Last Post: MrLeads

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020