快捷搜索:  汽车  科技

python随机密码生成程序10个(57个挑战-37-设计随机密码生成器)

python随机密码生成程序10个(57个挑战-37-设计随机密码生成器)import random def get_input(): #获取密钥串的长度,获取密钥串中特殊字符的个数,获取密钥串中数字的个数,并返回 pswdlength=input("What's the minimum length") speclength=input("How many special characters?") numblength=input("How many numbers?") return pswdlength speclength numblength def randomspecial():#返回随机特殊字符 lista=('~' "!" "@" "#" "$" "%&#

题目如下

python随机密码生成程序10个(57个挑战-37-设计随机密码生成器)(1)

解决思路如下:

找一个本子,把算法思路画下来。可以写的杂乱,后续把思路和流程图画清楚即可。

python随机密码生成程序10个(57个挑战-37-设计随机密码生成器)(2)

具体代码实现:

import random def get_input(): #获取密钥串的长度,获取密钥串中特殊字符的个数,获取密钥串中数字的个数,并返回 pswdlength=input("What's the minimum length") speclength=input("How many special characters?") numblength=input("How many numbers?") return pswdlength speclength numblength def randomspecial():#返回随机特殊字符 lista=('~' "!" "@" "#" "$" "%" "&" "*" "(" ")" "_" " " "-" "=") i=random.randint(0 len(lista)-1) return lista[i] def randomcharacter():#返回随机字母 listb=("a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z") j=random.randint(0 len(listb)-1) return listb[j] def producelist(pswdlength speclength numblength): #产生一个列表 list = {} pswdlength = int(pswdlength) speclength = int(speclength) numblength = int(numblength) leftlength = pswdlength - speclength- numblength for i in range(0 pswdlength): if speclength>0: list[i]=randomspecial() speclength = speclength - 1 elif numblength>0: list[i]=random.randint(0 9) numblength = numblength - 1 elif leftlength>0: list[i]=randomcharacter() leftlength = leftlength - 1 #print("字符串长度是{0}".format(len(list))) #debug 用 return list def randomlizelist(list): #交换算法,遍历key 随机和字典里面其它的key下的value进行交换。 listlength = len(list) for i in range(0 listlength-1): j = random.randint(0 listlength-1) temp = list[i] list[i] = list[j] list[j] = temp return list if __name__ == '__main__': pswdlength speclength numblength = get_input() list=producelist(pswdlength speclength numblength) list=randomlizelist(list) print("Your password is " end="") for i in range(0 len(list)): print(list[i] end="") #debug 用 print("")#debug 用 #debug 用 print(list)#debug 用

效果图:

python随机密码生成程序10个(57个挑战-37-设计随机密码生成器)(3)

猜您喜欢: