隨機產生7位數密碼


#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()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM