#!/usr/bin/env python# -*- coding: utf-8 -*-#用于生成随机数的模块: random# function chr () returns the corresponding ASCII character, Contrary to the Ord () effect. import random# generates a random number num = random.randint (1,100) #随机生成一个大写字母 between 1-100, and the ASCII code value 65-90 corresponds to a-zcap =  CHR (Random.randint (65,90)) #随机生成一个小写字母, the ASCII code value 97-122 corresponds to A-ZLOW = CAP = CHR ( Random.randint (97,122)) #下面定义一个验证码生成的函数def verification_code (): num = Random.randint (100,1000) capa = chr (Random.randint (65,90))  CAPB = CHR (Random.randint (65,90))     LOW = CHR (Random.randint ( 97,122)) vercode = capa + str (num) + capb + low return vercode #第二种定义def ver (): ver = [] for i in range (6): if i == random.randint (1,9): ver.append (Random.randint (1,9)) else: temp = random.randint (65,90) ver.append (Chr (temp)) code = ". Join (ver) return code print ' Verification Code:%s ' % verification_code () print ' captcha: ', ver () #第三种定义 (Recommended Practice) ' Randrange () The method returns a random number in the specified increment cardinality collection, and the cardinality default value is 1. random.randrange ([start,] stop [,step]) parameter start -- start value within the specified range, contained within the range. stop -- the end value within the specified range, not contained within the range. step -- specifies the increment cardinality. The Randrange () and Randint () differ from the random module by specifying a cardinality, which defaults to 1 "checkcode = " "For i iN range (6): current = random.randrange (0,9,2) if  CURRENT != I:        TEMP = CHR ( Random.randint (65,90)) else: temp = random.randint (0,9) checkcode += str (temp) Print checkcode
This article is from the "Fa&it-Q Group: 223843163" blog, please be sure to keep this source http://freshair.blog.51cto.com/8272891/1870789
Python randomly generated 6-digit verification Code