python3 生成隨機密碼 練習


from random import choice
import string

passwd_length = int(input('請輸入要生成的密碼長度:'))
passwd_count = int(input('請輸入要生成幾組密碼:'))

symbols = string.punctuation #通過string.punctuation獲取所有的字符 如:'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
number = string.digits #通過string.digits 獲取所有的數字的字符串 如:'0123456789'
Letter = string.ascii_letters #通過string.ascii_letters 獲取所有因為字符的大小寫字符串 'abc....zABC.....Z'

passwd = symbols + number + Letter #定義生成密碼是組成密碼元素的范圍 字符+數字+大小寫字母

def generate_passwd(*args,**kwargs):
passwd_lst = []
while (len(passwd_lst) < passwd_length):
passwd_lst.append(choice(passwd)) #把循環出來的字符插入到passwd_lst列表中
return ''.join(passwd_lst) #通過''.join(passwd_lst)合並列表中的所有元素組成新的字符串

if __name__ == '__main__':
for i in range(0,passwd_count):
print(generate_passwd())


免責聲明!

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



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