题目要求:
随机密码生成。编写程序,在26个字母大小写和9个数字组成的列表中随机生成10个8为的密码。
import random
def random_password():
#定义一个空列表
list1 = []
# 列用append()函数【在末尾追加】 把字母(字符类型)加入序列中
for i in range(65, 90):
list1.append(chr(i)) #65到90,计算机默认是数字,chr(i)是将数字转化为ASCII码表中的字符
for i in range(97, 122):
list1.append(chr(i))
list2 = [1, 2, 3, 4, 5, 6, 7, 8, 9] #数字列表
list = list1 + list2
n = 1 #从1开始
#生成10个8位的密码
while n < 11:
password = random.sample(list, 8) #从list列表中,随机选取8个数生成一个新的列表
# 把列表转化为字符串
password_middle = [str(i) for i in password] #将password列表中的元素全部转换成字符型
password_end = ''.join(password_middle) #将pass_middle列表转化成字符串
print("第{}个随机生成的密码是:{}".format(n, password_end))
n = n + 1
#调用函数
random_password()
运行结果: