随笔 - 258  文章 - 0  评论 - 0  阅读 - 32104

Python 练习 随机生成4位验证码

1
2
3
4
5
6
7
8
9
10
11
12
import random
if __name__ =="__main__":    #四位数字字母验证码的生成
    checkcode="" #保存验证码的变量
    for i in range(4):
        index=random.randrange(0,4#生成一个0~3中的数
        if index!=i and index +1 !=i:
            checkcode +=chr(random.randint(97,122))  # 生成a~z中的一个小写字母
        elif index +1==i:
            checkcode +=chr(random.randint(65,90) ) # 生成A~Z中的一个大写字母
        else:
            checkcode +=str(random.randint(1,9))  # 数字1-9
    print(checkcode)

 

 

posted on   季昂  阅读(1509)  评论(0编辑  收藏  举报
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示