day4_用集合生成8位密码的程序

import random
import string
count = input('请输入你要产生多少条密码:').strip()
passwords = set()
if count.isdigit():
while len(passwords) < int(count):
pwd = set(random.sample(string.ascii_letters + string.digits, 8)) # 把含有大写、小写字母和数字的list转成长度为8位的集合
set1 = set(string.ascii_uppercase).intersection(pwd) # 和大写字母取交集
set2 = set(string.ascii_lowercase).intersection(pwd) # 和小写字母取交集
set3 = set(string.digits).intersection(pwd) # 和数字取交集
if set1 and set2 and set3: # password这个集合里有大写字母、小写字母和数字
password = ''.join(pwd) + ' ' # 把集合拼成字符串
passwords.add(password)
f = open('passwords.txt', 'w')
f.writelines(passwords) # 把生成的密码一次性写到文件里
else:
print('你输入的不是数字,请重新输入!')
原文地址:https://www.cnblogs.com/laosun0204/p/8489542.html