1. 程式人生 > >Python 隨機生成規定數量密碼

Python 隨機生成規定數量密碼

#Python 3.6 (32-bit)

import random,string

count = input('請輸入你要產生多少條密碼:').strip() all_passwds = [] for i in range(int(count)):     num = random.sample(string.digits,1) #隨機取1位數字     lower = random.sample(string.ascii_lowercase,1) #隨機取1位小寫字母     upper = random.sample(string.ascii_uppercase,1) #隨機取1位大寫字母     other = random.sample(string.ascii_letters+string.digits,5) #隨機取5位     res = num+lower+upper+other #產生的8位密碼     res = ''.join(res)+'\n'     print('result:',res)     if res not in all_passwds: #判斷是否重複         all_passwds.append(res) with open('passwds.txt','a+') as fw:     fw.seek(0)     fw.writelines(all_passwds)