> 设计
简单编程随机生成密码的方法(随机密码生成编写程序)
导语:简单编程:随机生成密码
需求:利用26个字母和10个数字,随机生成10个8位密码
分析:得到26个字母和10个数字,再利用import random打印出随机数字
步骤:
10个数字可由列表推导表达式得到
26个字母通过格式化得到(&39;%97=a)
在使用import random模块打印出随机数字
(1)
import randomli_number=[str(i) for i in range(10)]li_char=[&39;% l for l in range(97,123)]li=li_number+li_charrow = 1while row<=10: col = 1 while col<=8: password=&39; index = random.randint(0,len(li)-1) password = password + li[index] print(password,end=&39;) col += 1 print() row += 1
(2)
import randomli_number=[str(i) for i in range(10)]li_char=[&39;%l for l in range(97,123)]li=li_number+li_charfor i in range(10): password=&39; for j in range(8): index = random.randint(0,len(li)-1) password = password + li[index] print(password)
(3)
import stringimport randomstring = string.ascii_lettersli_number=[str(i) for i in range(10)]string=string+&39;.join(li_number)passwords = set()while len(passwords) < 10: passwords.add(&39;.join(random.choices(string,k=8)))print(passwords)
本文内容由快快网络小德整理编辑!