A simple verification code program

Source: Internet
Author: User

A simple verification code program

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:

1 _ Author _ = "Zhang Peng" 2 import random 3 # import the random Module 4 5 code = 'authorization' 6 # define a verification code library, assign variable code 7 8 checkcode = ''9 # define an empty string and assign it to checkcode10 11 count = 012 # count the loop, the initial value is set to 013 14 while count <# Add a while loop. When the count is less than 6, it will keep repeating 16 17 I = random. choice (code) 18 # choice is a method in random, it is to randomly retrieve the value in the code and assign it to i19 20 checkcode + = i21 # assign the checkcode + I value to checkcode again, the length of the checkcode for each cycle is increased by 22 23 count + = 124 # The count + 1 value is assigned to count. Each cycle, the count value is increased by 125 else: 26 # else is a usage of the while LOOP, that is, what to do when the count is not less than 6 27 pass28 # What I wrote here is, when the count value is no less than 6, skip this loop 29 30 print (checkcode) 31 # print the checkcode at the end. The checkcode here will be different values every time it is run.

 

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 ):

 

1 _ Author _ = "Zhang Peng" 2 import random 3 # import the random Module 4 5 checkcode = ''6 # define an empty string, assigned to checkcode 7 8 for I in range (6): 9 # Here is a for loop, that is, let I loop in range (6), each loop gets the value 10, assigned to I; here range (6) = (,) 11 12 current = random. randrange () 13 # randrange is a method in random. random Number is obtained from range, assign the value to current14 15 if current <# Here an if statement is used. if current is less than 2, what should we do? 17 18 checkcode + = chr (random. randint () 19 # If current is equal to I, ran Dint is also a method in random. chr is the built-in function 20 in python # It refers to converting a number into a character corresponding to the ascll code table, 65 to 90 exactly corresponds to uppercase A to uppercase Z21 in the ascll Code # This function means that the chr retrieves A letter and adds the checkcode, assign a value to checkcode22 23 elif current> = 2 and current <4: 24 # elif. If current is greater than or equal to 2 and less than 4. 25 26 checkcode + = chr (random. randint (97,122) 27 # others are similar to the previous one, where 97 to 122 exactly corresponds to lowercase a to lowercase z28 29 else: 30 # if usage in ascll code, if current does not meet the preceding conditions, what to do? 31 32 checkcode + = str (random. randint () 33 # str is also a built-in function of python, which is to convert to a string. Here, we take any integer from 0 to 9 34 # And add checkcode, assign a new value to checkcode35 36 print (checkcode) 37 # print the checkcode

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.

Welcome to comments and provide valuable comments. This is also a very effective way to improve my skills.

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.