Python implements a simple verification code program, and python implements the verification code

Source: Internet
Author: User

Python implements a simple verification code program, and python implements the verification code

After talking about the random function, the teacher wrote it on his own. Although it was not so beautiful and intelligent as the example with the teacher, he also thought it out. So record it and the code needs to be continuously practiced by himself, in practice, we can improve it! Otherwise, most of us rely on self-taught people to learn at the same time. Another thing is, this time I listened to my teacher and added comments to my code. So I wrote comments to all the very simple code this time, and it was very plain-spoken, whether or not I had any experience with python, I believe that you can understand it carefully. If you try to write it out by yourself, it will be better:

Self-written:

_ Author _ = "Zhang Peng" import random # import the random module code = 'abcdefdhigkmlnopqrstuvwxyzabcdefdhigkmlnopqrstuvwxyz0123456789 '# define a verification code library, assign the variable codecheckcode = ''# to define an empty string and assign it to checkcodecount = 0 # to count the loop. Set the initial value to 0 while count <6: # Add a while loop, when count is less than 6, it will keep repeating I = random. choice (code) # choice is a method in random. It is to randomly retrieve the value in the code and assign it to I checkcode + = I # assign the checkcode + I value to checkcode again, one count + = 1 # value of count + 1 to count is added to the checkcode length of each loop. For each loop, 1 else is added to count: # else is a usage of the while LOOP, that is, what to do when count is not less than 6 # What I wrote here is, print (checkcode) # print the checkcode when count is not less than 6. The checkcode here will be different every time it is run.

Result:

M9Y7sE

The most awkward part of my code is that the pile of code defined by myself is very cumbersome. Let's look at the example provided by the teacher (this example is after my simple processing, only 6-digit verification codes with uppercase letters and numbers can be printed in the demo ):

_ Author _ = "Zhang Peng" import random # import the random module checkcode = ''# define an empty string and assign it to checkcodefor I in range (6 ): # Here is a for loop, that is, let I loop in range (6), get the value # In each loop, and assign it to I; here range (6) =, 4, 5) current = random. randrange () # randrange is a method in random. a random number is obtained from range () and assigned to current if current. <2: # Here an if statement is used, if current is less than 2, what should I do? checkcode + = chr (random. randint () # If current is equal to I, randint is also a method in random, and chr is a built-in function in python # It means to convert a number The characters corresponding to the ascll code table, 65 to 90 exactly correspond to uppercase letters A to uppercase Z in the ascll Code # This function means that the chr extracts A letter and adds checkcode, re-assign the value to checkcode elif current> = 2 and current <4: # elif. If current is greater than or equal to 2 and less than 4, what should be done. Checkcode + = chr (random. randint (97,122) # others are similar to the previous one, where 97 to 122 exactly corresponds to lowercase a to lowercase z else: # if usage in ascll code. if current does not meet the preceding conditions, what is checkcode + = str (random. randint () # str is also a python built-in function, which is to convert to a string. Here, it is to take any integer from 0 to 9 # And add checkcode, assign a value to checkcodeprint (checkcode) # print the checkcode

Running result:

vgW2u3

After I added it myself, I felt that I had added an elif, but it was a little bulky. I hope I can work out clearer code in the future.

Summary

The above is all about Python's implementation of a simple verification code program. I hope it will be helpful to you. Interested friends can continue to refer to this site: python programming django can be registered only once within 10 minutes to implement the same ip address, a simple understanding of several functions in Python, Python algorithm output 1-9 array to form all the calculation formula of 100, etc, you are welcome to leave a message to give us valuable comments and share them with us. Thank you for your support!

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.