编写程序,在52个大小写英文字母和10个数字(0~9)组成的列表中随机生成10个8位密码,并将它们显示出来。
import random
# 1.全密码字符串
txt = "01233456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
# 2.字符串转列表
alpha_num_list = list(txt)
# 3.生成10组密码
for i in range(10):
password = ""
# 8位数
for j in range(8):
password += random.choice(alpha_num_list)
# 打印该组随机密码
print(password)
import string
import random
passwords = []
# 使用的字符串,数字+下划线+字母
s = string.digits + string.ascii_letters + "_"
# 因为数字和字母不能连续,也就是数字的下一位只能是字母或者下划线.
s1 = string.digits + "_" # 不包含字母
s2 = string.ascii_letters + "_" # 不包含数字
s3 = string.digits + string.ascii_letters # 不包含下划线
while len(passwords) < 10:
password = ""
for i in range(8):
if i == 0:
c = random.choice(s) # 先选取一个字符
password += c
else:
last = password[i-1] # 获取上一个字符
if last.isdigit(): # 如果上一个字符是数字,就不能再随机选取数字了
c = random.choice(s2)
elif last == "_":
c = random.choice(s3)
else:
c = random.choice(s1)
password += c
if password not in passwords: # 防止密码重复
passwords.append(password)
print("生成的密码: {}".format(passwords))
import random, string
for i in range(10):
num = string.ascii_letters + string.digits
print("".join(random.sample(num, 8)))
如果对你有帮助,可以点击我这个回答右上方的【采纳】按钮,给我个采纳吗,谢谢
import random
l=['a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z', 'A', 'B',
'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9']
password=''
for i in range(8):
password+=random.choice(l)
print(password)