password strength code help python

时间:2016-02-12 22:12:09

标签: python

so i am writing code in python to tell you your password strength but it is not doing as i say and keeps saying invalid password password unless you use only numbers

Sub TestError()
    On Error GoTo errError

    'Code that should raise an error and send to errError
    Excel.Application.Workbooks.Open "lakdfjldkj"
    Exit Sub
errError:
    MsgBox "Code didn't break"
    Resume
End Sub

any help would be appriciated

2 个答案:

答案 0 :(得分:0)

首先,Python是一种非常宽容的语言,你可以使用其他语言无法做到的很多东西,但是:

numbers=passwordentered.count("1"and"2"and"3"and"4"and"5"and"6"and"7"and"8"and"9")

不行。 Python将评估括号内的所有内容作为布尔表达式,这行代码基本上归结为:

numbers=passwordentered.count("9")

其他检查也是如此。要从中获得您真正想要的东西,您应该使用某种循环。你可以使用这样的东西:

numbers = sum( ch.isdigit() for ch in passwordentered )

这将为您计算密码字符串中找到的位数。您可以修改它来计算小写和大写字母的数量。

最后,你应该尝试清理你的if语句,你可以更整齐地分解它们并使它更具可读性。

答案 1 :(得分:0)

如果这样写的那些count函数不会起作用。

尝试替换它:

numbers=passwordentered.count ("1"and"2"and"3"and"4"and"5"and"6"and"7"and"8"and"9")
lowerletters=passwordentered.count ("a"and"b"and"c"and"d"and"e"and"f"and"g"and"h"and"i"and"j"and"k"and"l"and"m"and"n"and"o"and"p"and"q"and"r"and"s"and"t"and"u"and"v"and"w"and"x"and"z")
higherletters=passwordentered.count ("A"and"B"and"C"and"D"and"E"and"F"and"G"and"H"and"I"and"J"and"K"and"L"and"M"and"N"and"O"and"P"and"Q"and"R"and"S"and"T"and"U"and"V"and"W"and"X"and"Z")

以下内容:

numbers = sum(typ in "0123456789" for typ in passwordentered)
lowerletters = sum(typ in "abcdefghijklmnopqrstuvwxyz" for typ in passwordentered)
higherletters = sum(typ in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" for typ in passwordentered)

或以下的详细方法:

numberlist = "0123456789"
Lletterlist = "abcdefghijklmnopqrstuvwxyz"
Hletterlist = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

numbers=0
lowerletters=0
higherletters=0
for f in xrange(0, x):
    if numberlist.find(passwordentered[f]) != -1:
        numbers = numbers+1
    if Lletterlist.find(passwordentered[f]) != -1:
        lowerletters = lowerletters+1
    if Hletterlist.find(passwordentered[f]) != -1:
        higherletters = higherletters+1