python - Checking ISBN numbers -


this code:

def isisbn(n):     if len(n)!= 10:         return false     else:         d1=int(n[0])*1         d2=int(n[1])*2         d3=int(n[2])*3         d4=int(n[3])*4         d5=int(n[4])*5         d6=int(n[5])*6         d7=int(n[6])*7         d8=int(n[7])*8         d9=int(n[8])*9     d10=(d1+d2+d3+d4+d5+d6+d7+d8+d9)     num=d10%11     print(d10,num)     if num==10:         return true     else:         return false 

here test cases teacher gave us:

>>> isisbn('020103803x') true >>> isisbn('0540122068') true >>> isisbn('020108303x') false >>> isisbn('0540122069') false 

the code fails test '0540122068' because output false, don't know why.

don't forget 10th value , check modulo equivalence 0:

def isisbn(n):     if len(n)!= 10:         return false     else:         d1=int(n[0])*1         d2=int(n[1])*2         d3=int(n[2])*3         d4=int(n[3])*4         d5=int(n[4])*5         d6=int(n[5])*6         d7=int(n[6])*7         d8=int(n[7])*8         d9=int(n[8])*9         if n[9] == 'x':             d10 = 10         else:             d10 = int(n[9])         d10 = d10*10         d11=(d1+d2+d3+d4+d5+d6+d7+d8+d9+d10)         num=d11%11         if num==0:             return true         else:             return false  isisbn("3680087837") 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -