1. 程式人生 > >python3 生成隨機密碼 練習

python3 生成隨機密碼 練習

range 大小寫 put 密碼 inpu ... __name__ 字符 獲取

from random import choice
import string

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

symbols = string.punctuation #通過string.punctuation獲取所有的字符 如:‘!"#$%&\‘()*+,-./:;<=>[email protected][\\]^_`{|}~‘
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())

python3 生成隨機密碼 練習