A Python applet that calculates the check digit of the ID card number _python

Source: Internet
Author: User
Tags function prototype

S = Sum (Ai * Wi), i=0,....... 16 (now the ID card number is 18-bit long, the last one is check, 15 ID card number does not seem to need)

AI corresponding to the ID number, WI is used for weighted calculation of the value, it is a series of fixed values, it should be based on some kind of rules to get the best randomness, WI of the following:

7 9 10 5
8 4 2 1
6 3 7 9
10 5 8 4 2

After a weighted calculation, get an S, use this s to die 11, take the remainder value, and then check the table to get the check bit, this index table is as follows:

0-----1
1-----0
2-----X
3-----9
4-----8
5-----7
6-----6
7-----5
8-----4
9-----3
Ten-----2

The program code is as follows:

The import sys

Wi = [7, 9, 5, 8, 4, 2, 1, 6, 3, 7,9, 5, 8, 4, 2]
indextable = {#此处实际是无需使用字典的, using an array of 11 elements can , the array holds
        0: ' 1 ', #相应位置的号码, but this also demonstrates the use of Python's advanced data structure
        1: ' 0 '
        , 2: ' X ',
        3: ' 9 ',
        4: ' 8 ',
        5: ' 7 ', 
   
    6: ' 6 ',
        7: ' 5 ',
        8: ' 4 ',
        9: ' 3 ', ' 2 '
    }
No = []
sum = 0
if (Len (sys.argv[1 :][0])!=:
    print "error number"
    Sys.exit () for
x in sys.argv[1:][0]:
        no.append (x) to
I in Range:
    sum = sum + (int (no[i]) * Wi[i])
Index = sum%
print "So, your indicates parity is:%s"% (i Ndextable[index])


   

Run the program in the following way:

#python getparity.py Your-indentity-number-but-except-the-last-number

Oh, my God, Python's built-in data structure is so powerful and easy to use, it's getting more and more fascinating. Continue diving~

With functions encapsulated, the improved code is as follows:

Import sys

if __name__!= ' __main__ ':
  print "Cannot run in module"
  sys.exit ()

Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7,9, 5, 8, 4, 2]
indextable = {
    0: ' 1 ',
    1: ' 0 ', 2:
    ' x ',
    3: ' 9 ', 4
    : ' 8 ',
    5 : ' 7 ',
    6: ' 6 ',
    7: ' 5 ',
    8: ' 4 ',
    9: ' 3 ', '
    2 '
  }

def check (identity):
  if (Len ( Identity) = = 0:
    print "Please input your identity number"
    sys.exit ()
  elif (len (identity[0))!= 17):
    print "error number"
    Sys.exit ()

def Calculate (identity):
  No = []
  sum = 0 for
  x in Identity[0]: #这个方法是很笨拙的, direct use of No = List (identity[0]) can achieve the same purpose
    no.append (x) for

  I in Range (a):
    sum = sum + (int (no[i]) * Wi[i])

  Index = sum%
  Indextable[index]

check (sys.argv[1:]) result
= Calculate (sys.argv[1:]) 

print ' So, your indicates parity:%s '% (result)

Forget about the function prototype, where you don't need to indicate the return value type, and you don't need to specify the type of parameter.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.