用簡單的方法生成隨機性較大的密碼
僅用20行代碼隨機生成密碼
核心思路:利用random模塊
random模塊隨機生成數字,大小寫字母,循環次數
while循環+隨機生成的循環次數——>隨機plus++
大寫字母ASKII碼在65-90之間
小寫字母Askll碼在97-122之間
最終效果: x個大寫字母+y個數字+z個小寫字母(x,y,z均隨機)
隨機性相較于以往單調的 小寫+數字+大寫+小寫+數字+大寫… 循環有所提升
import randomprint("隨機數生成”)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行代碼),希望對大家有所幫助!
新聞熱點
疑難解答