用random模块实现验证码

#! /usr/bin/env python3
import random

checkcode = ""
## 全部为数字的验证码
# for i in range(4):
#     current = random.randrange(0,6)
#     checkcode += str(current)
# print(checkcode)

## 有数字、字母的验证码
for i in range(6):
    current = random.randrange(0, 4)   ## 类似于[start, end)
    if current == i:
        tmp = chr(random.randint(65, 90))  ##等价于 random.randrange(65, 91)
    else:
        tmp = random.randint(0, 9)
    checkcode += str(tmp)
print(checkcode)

 

posted @ 2017-12-30 19:30  wang_zai  阅读(213)  评论(0)    收藏  举报