问题描述
我正在用字典构建自己的密码生成器,并检查内部每种类型是否都有字符。它工作正常,但我认为我对支票进行了编码。
如果有一种更好的编码方法,您是否有想法? 并且是否有一种方法可以摆脱检查,如果它已经准备就绪,那么它就不会检查其他类型?
PS:我想定义自己的二手低价/高档/特殊商品/数字,以便始终避免添加不喜欢的字符。
chars = ""
alpha_lowers = "abcdefghijklmnopqrstuvwxyz"
alpha_uppers = "ABCDEFGHIJKLMnopQRSTUVWXYZ"
specials = "$%&/()=?.,"
nums = "0123456789"
dictionary = {
"a" : "anton","b" : "berta","c" : "caesar","d" : "dora","e" : "emil","f" : "friedich","g" : "gustav","h" : "hotel","i" : "india","j" : "julia","k" : "kilo","l" : "ludwig","m" : "marta","n" : "nordpol","o" : "otto","p" : "paula","q" : "quelle","r" : "richard","s" : "iegfried","t" : "theodor","u" : "ulrich","v" : "viktor","w" : "willhelm","x" : "xaver","y" : "ypsilon","z" : "zeppelin","A" : "Anton","B" : "Berta","C" : "Caesar","D" : "Dora","E" : "Emil","F" : "Friedrich","G" : "Golf","H" : "Hotel","I" : "India","J" : "Julius","K" : "Kilo","L" : "Ludwig","M" : "Marta","N" : "nordpol","O" : "otto","P" : "Paula","Q" : "Quelle","R" : "Richard","S" : "Siegfried","T" : "Theodor","U" : "Ulrich","V" : "Viktor","W" : "Willhelm","X" : "Xaver","Y" : "Ypsilon","Z" : "Zeppelin","$" : "Dollar","%" : "Prozent","&" : "Und","/" : "Schräg","(" : "Klammer auf",")" : "Klammer zu","=" : "Gleich","?" : "Fragezeichen","." : "Punkt","," : "Beistrich","0" : "Null","1" : "Eins","2" : "Zwei","3" : "Drei","4" : "Vier","5" : "Fünf","6" : "Sechs","7" : "Sieben","8" : "Acht","9" : "Neun"
}
all_chars = True
# Kleinbuchstaben hinzufügen // Adding Lowers
chars = chars + alpha_lowers
# Großbuchstaben hinzufügen // Adding uppers
chars = chars + alpha_uppers
# Spezial-Zeichen hinzufügen // Adding Specials
chars = chars + specials
# Nummern hinzufügen // Adding Nums
chars = chars + nums
# PW-Menge definieren // How many PW
password_n = 10
# PW-Länge definieren // Password length
password_len = 32
#--------------------------------------------------------------
def password_gen(length):
# Generating PW
password = ""
for i in range (0,length):
password = password + random.choice(chars)
# Check if there is a Char from every type
if all_chars == True:
in_alpha_lowers = False
in_alpha_uppers = False
in_specials = False
in_nums = False
for c in password:
if in_alpha_lowers == False:
if c in alpha_lowers:
in_alpha_lowers = True
if in_alpha_uppers == False:
if c in alpha_uppers:
in_alpha_uppers = True
if in_specials == False:
if c in specials:
in_specials = True
if in_nums == False:
if c in nums:
in_nums = True
if in_alpha_lowers == False or in_alpha_uppers == False or in_specials == False or in_nums == False:
print(password + " is not valid! New Passwort will be generated!" + "\n")
return "invalid"
else:
return password
else:
return password
#--------------------------------------------------------------
i = 1
while i <= password_n:
password = ""
sentence = ""
password = password_gen(password_len)
if password != "invalid":
print("valid Passwort")
i += 1
for c in password:
sentence = sentence + " " + dictionary[c]
print(password)
print(sentence.lstrip() + "\n")
解决方法
您是否有想法,如果有一种更好的编码方式。
我建议看一下set
的操作intersect
。例如,您可以按照以下方式检查密码是否包含小写字母:
alpha_lowers = "abcdefghijklmnopqrstuvwxyz"
password = "password"
haslower = bool(set(password).intersection(alpha_lowers))
print(haslower) # True
说明:我确实使alpha_lowers
和password
都通用一组字母,然后将其转换为bool,如果False
为空且否则set
。
是否有办法摆脱检查是否已经准备就绪,以便不检查其他类型?
由于您已经具有功能,可能在检查未通过后立即True
。