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


Messages In This Thread
What's wrong with this code? - by kumaaaa - Sep-28-2019, 09:14 AM
RE: What's wrong with this code? - by Gribouillis - Sep-28-2019, 09:38 AM
RE: What's wrong with this code? - by buran - Sep-28-2019, 10:51 AM
RE: What's wrong with this code? - by kumaaaa - Sep-28-2019, 11:01 AM
RE: What's wrong with this code? - by kumaaaa - Sep-28-2019, 11:43 AM
RE: What's wrong with this code? - by perfringo - Sep-28-2019, 11:29 AM
RE: What's wrong with this code? - by DeaD_EyE - Sep-28-2019, 12:06 PM

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 628 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Something wrong with my code FabianPruitt 5 1,054 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,827 Mar-27-2023, 07:38 AM
Last Post: buran
  Video recording with Raspberry Pi - What´s wrong with my python code? Montezuma1502 3 1,447 Feb-24-2023, 06:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 2,095 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,983 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Wrong code in Python exercise MaartenRo 2 1,621 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,706 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  VS Code debugger using wrong Python environment topfox 0 2,641 Jun-09-2021, 10:01 AM
Last Post: topfox
  What is wrong with my code??? MrLeads 15 5,354 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