Python generates random verification code

Source: Internet
Author: User

First, generate random verification code (pure numbers and letters plus numbers):

import randomimport stringcheckcod=‘‘for i in range(5):  #5位验证码    ‘‘‘    #纯数字验证码    #随机值1-9取可以保证5位,如果是1-12就会出现5位以上验证码    current=random.randint(1,9)    #i数据类型转换成字符串类型    #checkcod+=str(i)    checkcod+=str(current)    ‘‘‘    #数字加字母验证码   循环5次:猜的值和当前循环i值是否相等    current=random.randrange(0,5)    if current == i:                        #猜的值与当前i循环值相等就会执行下面tmp值为字母        tmp=chr(random.randint(65,90))                #把十进制数字转换成字母用chr(65到90是获取大写字母        #chr(65)是大A chr(90)是大写        #获取65到90用random.randint()           else:        # 否则就是猜的值与当前i值不相等,就会是纯数字        tmp=random.randint(0,9)    checkcod+=str(tmp)print(checkcod)

Second, generate random verification code (letter plus number):

import randomcheckcode = ‘‘for i in range(4):    current = random.randrange(0,4)    if current != i:     #!=  不等于 - 比较两个对象是否不相等        temp = chr(random.randint(65,90))    else:        temp = random.randint(0,9)    checkcode += str(temp)print (checkcode)

The value obtained by the! = method is a letter + number, while the = = method is sometimes circulated as a number + letter, sometimes as a pure number.

Python generates random verification code

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.