小记一次考题:生成包含大写字母、小写字母、数字的8位密码

随机验证码生成算法
本文介绍了一种使用Python实现的随机验证码生成算法,通过随机选择大小写字母和数字,确保生成的验证码满足特定的复杂度要求,如至少包含一个大写字母、一个小写字母和一个数字。该算法使用了ASCII码范围内的字符,并通过正则表达式进行匹配验证。

思路一:str=‘abcd.....xyz0123456789ABCD....XYZ’     

思路二:str1='abcd...'  str2='ABCD....XYZ'   str3='0123456789'

思路三:引入string    使用   string.ascii_lowercase 等

思路四:引入ASCII    使用随机以及正则      目前感觉是比较正确的    有待优化

 1 # -*- coding: utf-8 -*-
 2 # @Time    : 2018/10/8 16:24
 3 # @Author  : wangyafeng
 4 # @Email   : 279949848@qq.com
 5 # @Software: PyCharm
 6 
 7 
 8 import random,re
 9 
10 checkcode = ''
11 for i in range(8):
12     current = random.randrange(0,100)
13     # 字母
14     if current%2==0:
15         tmp=chr(random.randint(65,90))
16         checkcode += str(tmp)
17     elif current%3==0:
18         tmp=chr(random.randint(97,122))
19         checkcode += str(tmp)
20     #数字
21     else:
22         tmp=random.randint(0,9)
23         checkcode += str(tmp)
24 
25 # matchObj=re.match('^[A-Za-z0-9]{8}$',checkcode)
26 matchObj=re.match('^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9]).{8}$',checkcode)
27 if matchObj:
28     print(checkcode)
29 else:
30     print("请重新生成")

 

今天彻底解决这个问题 ,尚待代码继续优化,但是终归是解决了  这个思路还是比较简单的

 1 import random
 2 listc=[]
 3 listd=[]
 4 liste=[]
 5 for i in range(65,91):
 6     listc.append(chr(i))
 7 for j in range(97,123):
 8     listd.append(chr(j))
 9 for k in range(48,58):
10     liste.append(chr(k))
11 
12 while True:
13     a=random.randint(1,6)
14     if 6-a>1:
15         b=random.randint(1,6-a)
16     else:
17         b=random.randint(1,1)
18     if  6-a-b>1:
19         c=random.randint(1,6-a-b)
20     else:
21         c=random.randint(1,1)
22 
23     chart=random.sample(listc,a)+random.sample(listd,b)+random.sample(liste,c)
24     chart=''.join(chart)
25     if len(chart) ==6:
26         print(chart)
27         break

 

转载于:https://www.cnblogs.com/wangyafeng/p/9755351.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值