编写程序在26个字母大小写和9个数字组成的列表中随机生成10个8位密码。
import random
num_ls = [] # 创建数字、小写字母、大写字母空列表
str_ls = []
STR_ls = []
for i in range(ord('1'),ord('9') + 1): # 将数字对应的unicode码添加到对应list中
num_ls.append(i)
for i in range(ord('a'),ord('z') + 1): # 将小写字母对应的unicode码添加到对应list中
str_ls.append(i)
for i in range(ord('A'),ord('Z') + 1): # 将大写字母对应的unicode码添加到对应list中
STR_ls.append(i)
ls = num_ls + str_ls + STR_ls
def passWord(ls):
"""随机从列表中选取8个unicode码,并转化成字符"""
s = []
while len(s) < 8:
s.append(random.choice(ls))
for i in s:
print(chr(i),end = '')
def passWord_group(ls):
g = []
while len(g) < 10:
g.append(passWord(ls))
print('')
return g
passWord_group(ls)
语言程序设计冲刺试卷-试卷模拟C-第四题
原答案有误,正确的答案为:
from random import *
seed(0x1010)
s = 'abcdefghijklmnopqistuvwxyzABCDEFGHIJKLMNOPQISTUVWXYZ0123456789!@#$%^&*'
ls = []
ex = ' '
while len(ls) < 10:
pwd = ' '
for i in range(10):
pwd += s[randint(0,len(s)-1)]
if pwd[0] in ex: #原书中的答案,此行没缩进
continue
else:
ls.append(pwd)
ex += pwd[0]
with open("python二级冲刺试卷\\模拟试卷C\\随机密码.txt",'w',encoding = 'utf-8') as f:
f.write('\n'.join(ls))
网友评论