您的位置:首页 > 编程语言 > Python开发

python随机生成大小写字母数字混合密码(仅20行代码)

2020-02-13 11:30 2126 查看

用简单的方法生成随机性较大的密码

仅用20行代码随机生成密码

核心思路:利用random模块

random模块随机生成数字,大小写字母,循环次数

while循环+随机生成的循环次数――>随机plus++

大写字母ASKII码在65-90之间

小写字母Askll码在97-122之间

最终效果: x个大写字母+y个数字+z个小写字母(x,y,z均随机)

随机性相较于以往单调的 小写+数字+大写+小写+数字+大写… 循环有所提升

import random
print("随机数生成”)
time=random.randint(1,2)
while time:
time1=random.randint(1, 3)
time2=random.randint(1, 2)
time3=random.randint(1, 3)
while time1:
a= random.randint(65,90)
print("%c"%a,end="")
time1-=1
while time 2:
c= random.randint(0,99)
print("%d"%c,end="")
time2-=1
while time3:
b= random.randint(97,122)
print("%c"%b,end="")
time 3-=1
time-=1

补充:用Python随机生成一个六位验证码(验证码由数字和字母组成(大小写字母))

import random
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
这里要用到random函数中的随机生成一个区间的整数 randint 函数模块
第一次知道循环可以这样用 for _ in range():
hhh
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
def generate_code(code_len = 6):
all_char = '0123456789qazwsxedcrfvtgbyhnujmikolpQAZWSXEDCRFVTGBYHNUJIKOLP'
index = len(all_char) + 1
code = ''
for _ in range(code_len):
num = random.randint(0,index)
code += all_char[num]
return code
print(generate_code())

总结

以上所述是小编给大家介绍的python随机生成大小写字母数字混合密码(仅20行代码),希望对大家有所帮助!

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐