1.生成9位字母的密码

使用random.choice函数,此函数需要一个序列,因此给定一个序列包含a-z,A-Z

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
#step1:生成序列
import random s1=''
for i in range(97,123): s1+=chr(i)+chr(i-32) #step2: 生成密码
password=''
for i in range(9): password+=random.choice(s1)#从s1序列中随机选取一个元素
print('9位数的密码为:',password)

2:生成9位数字和字母的密码,密码可能随机出现数字和字母
此题在上一题的基础上先生成一个序列包含所有字母和数字,然后使用random.choice()函数

import random s1=''
for i in range(97,123): s1+=chr(i)+chr(i-32) s1+='0123456789'
print(s1) #生成包含小写字母和数字的序列 #另外一个写法
import string s1 = string.ascii_letters+string.digits password=''
for i in range(9): password+=random.choice(s1) print('随机密码为:',password)

3.检测密码强度

c1 : 长度>=8
c2: 包含数字和字母
c3: 其他可见的特殊字符
强密码条件:满足c1,c2,c3
中密码条件: 只满足任一2个条件
弱密码条件:只满足任一1个或0个条件

思路:先将c1,c2,c3三个条件写成函数,以Ture和False返回,True为满足,False为不满足

step1.写出3个条件函数

def check_len(password): if len(password)>=8: return True #长度大于8则为True
    else: return False def check_letter_type(password): import string result =False for i in string.ascii_letters: #大小写字母
        if i in password: #密码中包含字母
            for j in string.digits: #数字
                if j in password: #密码中包含数字
                    result =True return result def check_punctuation(password): import string result = False for i in string.punctuation: if i in password: #密码中包含特殊字符
            result =True return result

check_len检查密码长度,check_letter_type检查密码中是否包含字母和数字,check_punctuation检查密码中是否包含特殊字符

step2:检查密码满足几个条件

def pass_verify(password): count = 0 #使用count来记录password满足几个条件
    if check_len(password): count +=1
    if check_letter_type(password): count +=1
    if check_punctuation(password): count +=1
    if count==3: print("强密码") elif count ==2: print('中密码') else: print('弱密码') pass_verify(password)

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄