#1、隨機從所有的字符隨機取7位 #2、再分別和所有大小寫字母、數字、特殊字母取交集 #1、第二種思路 # 從大寫字母 upper_Case ='A-Z' lower_Case = 'a-z' digits='0-9' puc='23$@$@$' import random import string num = input('請輸入要產生多少條密碼:').strip() passwords = [] if num.isdigit(): num = int(num) while len(passwords) != num: p1 = random.sample(string.ascii_letters+string.digits+string.punctuation,7) #隨機取7位 print(p1) if set(p1) & set(string.ascii_lowercase) and set(p1) & set(string.ascii_uppercase) \ and set(p1) & set(string.digits) and set(p1) & set(string.punctuation): password = ''.join(p1)#把密碼變成字符串 if password not in passwords: passwords.append(password) else: print('請輸入數字') f = open('passwrods.txt','w') for p in passwords: f.write(p+'\n') f.close()
