A Python applet used to calculate the checkpoint of an ID card number

Source: Internet
Author: User
Having nothing to do, I want to write a practical Python applet, how to check the computer's ID card number, such an article on the internet, here is a brief introduction of S = Sum (Ai * Wi), I = 0 ,....... 16 (currently, all ID card numbers are 18 characters long. The last digit is the verification digit, and the 15-digit ID card number does not seem to have been used)

Ai corresponds to the ID card number, and Wi is the value used for weighted calculation. It is a fixed string of values, which should be obtained according to certain rules for obtaining the best randomness, the Wi is obtained as follows:

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

After weighting, get a second, use this second to mod 11, get the remainder value, and then look up the table to get the check bit. The 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
10 ----- 2

The program code is as follows:

Import sysWi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] IndexTable = {# You do not need to use a dictionary here. You can use an array containing 11 elements and store 0: '1' in the array. # The number at the corresponding position, however, this demonstrates the use of the Python advanced data structure: 1: '0', 2: 'X', 3: '9', 4: '8', 5: '7', 6: '6', 7: '5', 8: '4', 9: '3', 10: '2'} No = [] sum = 0if (len (sys. argv [1:] [0])! = 17): print "error number" sys. exit () for x in sys. argv [1:] [0]: No. append (x) for I in range (17): sum = sum + (int (No [I]) * Wi [I]) Index = sum % 11 print "So, your indicates parity is: % s "% (IndexTable [Index])

Run the program as follows:

# Python getParity. py your-indentity-number-but-modified t-the-last-number

My God, the built-in data structure in Python is so powerful and easy to use that it is getting more and more fascinated. Continue to ping ~

The improved code is as follows:

Import sysif _ name __! = '_ Main _': print "Cannot run in module" sys. exit () Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 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', 10: '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]: # This method is clumsy, you can directly use No = list (identity [0]) to achieve the same purpose No. append (x) for I in range (17): sum = sum + (int (No [I]) * Wi [I]) index = sum % 11 return IndexTable [Index] check (sys. argv [1:]) result = calculate (sys. argv [1:]) print "So, your indicates parity is: % s" % (result)

Forget the function prototype. You do not need to specify the type of the returned value or the data type of the 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.