A Python applet that calculates the checkpoint of the ID card number

Source: Internet
Author: User

A Python applet that calculates the checkpoint of the ID card number

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 of Python is so powerful and easy to use that it is getting more and more fascinated. Continue to diving ~

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.


A python Applet

=

According to past observations, this kind of GUI is rarely done...

Help me write a python Applet

First = int (input ("Please enter the number of Epidemic cards (4-6 ):"))
Second = 2
For test1 in range (first-4 ):
For test2 in range (6-first ):
Print ("start: No epidemics, Infection Rate = 2 ")
For I in range (first ):
Print ("when Epidemics = % d, Infection Rate = % d" % (I + 1, second ))
Second + = 0.5
Break
Break

In the first for loop, the number smaller than 4 is excluded, and in the second for loop, the number greater than 6 is excluded. Then the third for loop implements the main function of the program. Because the third for loop only needs to be executed once, two breaks are used to exit the outer two-layer loop and all programs are ended.
Int (float) is used implicitly in the print function.

Soon after I learned Python, I had not mastered the features well and should have a more concise method. But now I only think of this.

Related Article

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.