简体   繁体   English

Python密码检查器帮助调试

[英]Python Password Checker Help Debugging

So in my computer programming class we've been tasked with making the generic password strength checker, mine works and checks the length correctly but for whatever reason the 2nd part doesn't work properly and I can't figure out why.因此,在我的计算机编程课程中,我们的任务是制作通用密码强度检查器,我的工作正常并正确检查长度,但无论出于何种原因,第二部分无法正常工作,我无法弄清楚原因。

                            password = input("Please enter a password between 6 - 12 characters long: ")


                            while len(password) in range(6, 13) == False:
                                if len(password) < 6:
                                    print("Password too low!")
                                    password = input("Please enter a password between 6 - 12 characters long: ")
                                elif len(password) > 12:
                                    print("Password too high!")
                                    password = input("Please enter a password between 6 - 12 characters long: ")
                            Upper = 0
                            Lower = 0
                            Numerical = 0
                            for char in password:
                                if char == char.isupper():
                                    Upper = Upper + 1
                                elif char == char.islower():
                                    Lower = Lower + 1
                                elif char == char.isnumeric():
                                    Numerical = Numerical + 1

                            Strength = int(Upper + Lower + Numerical)

                            if Strength == 1:
                                print("Password is weak!")
                            elif Strength == 2:
                                print("Password is medium!")
                            elif Strength == 3:
                                print("Password is strong!")

Any help would be greatly appreciated, I browsed other similar problems but with something specific it's kind of difficult.任何帮助将不胜感激,我浏览了其他类似的问题,但有一些具体的问题有点困难。

EDIT: Removing the parenthesis from my while loop will do nothing, it works the same anyway.编辑:从我的 while 循环中删除括号不会做任何事情,无论如何它的工作原理是一样的。 The in range part makes no difference, I still get the error:范围内部分没有区别,我仍然收到错误:

Traceback (most recent call last): File "C:\\Users\\Owen\\Desktop\\Password Checker.py", line 21, in for char in password():回溯(最近一次调用):文件“C:\\Users\\Owen\\Desktop\\Password Checker.py”,第 21 行,在密码()中的字符中:

There are a lot of little issues you can fix with this section of code so I can't really tell you what is producing the improper output.您可以使用这部分代码修复很多小问题,因此我无法真正告诉您是什么导致了不正确的输出。

1) as Ken stated: 1) 正如肯所说:

len(password) != range(6, 12) will always return true. len(password) != range(6, 12)将始终返回 true。 This is because you are comparing an int to a list.这是因为您正在将 int 与列表进行比较。

range(6, 12) gives you [6, 7, 8, 9, 10, 11] so the length of the password can never be equal to that statement. range(6, 12)为您提供[6, 7, 8, 9, 10, 11]因此密码的长度永远不能等于该语句。 However, if you use len(password) in range(6, 12) the length of the password is compared to each value in the list until there is a match (if there is one).但是,如果您len(password) in range(6, 12)使用len(password) in range(6, 12)的长度将与列表中的每个值进行比较,直到匹配为止(如果有)。 One more thing, using range(a, b) only includes numbers from a to b-1.还有一件事,使用 range(a, b) 只包括从 a 到 b-1 的数字。 So you really want: len(password) in range(6, 13)所以你真的想要: len(password) in range(6, 13)

2) The logic of the while loop should change with changes from my first point. 2) while 循环的逻辑应该随着我第一点的变化而变化。 If len(password) in range(6, 13) evaluates to True, then the while statement won't loop and you will move on to the next section.如果len(password) in range(6, 13)计算结果为 True,则 while 语句将不会循环,您将进入下一部分。 You can delete the else section and change your elif to else .您可以删除else部分并将您的elif更改为else

3) You can get rid of LengthLow and LengthHigh since you aren't using them anywhere. 3) 你可以去掉LengthLowLengthHigh因为你没有在任何地方使用它们。

4) password is a string so you can't call it with the () operator. 4) 密码是一个字符串,所以你不能用 () 运算符调用它。 Your for loop should be for char in password:你的 for 循环应该是for char in password:

5) Use char.isupper() , char.islower() , or char.isnumeric() to check if they are numeric values 5) 使用char.isupper()char.islower()char.isnumeric()来检查它们是否是数值

6) You don't need to store Upper , Lower , and Numerical separately since you are just adding them together at the end anyways. 6) 您不需要分别存储UpperLowerNumerical因为无论如何您只是在最后将它们加在一起。 You can actually accomplish this with a one liner if you are familiar with list comprehensions如果您熟悉列表推导式,您实际上可以使用单行代码完成此操作

strength = sum([1 if c.isupper() or c.islower() or c.isnumeric else 0 for c in password])

Although, it seems to me that this is all the same as c.isalnum() for alphanumeric.虽然,在我看来,这与字母数字的 c.isalnum() 完全相同。 So write,所以写,

strength = sum([1 if c.isalnum() else 0 for c in password])

7) I don't know anything about password strength calculation, but I think special characters should be considered as well. 7)我对密码强度计算一无所知,但我认为也应该考虑特殊字符。 In that case c.isalnum() won't be the magic all in one that it is in your current code.在这种情况下, c.isalnum()不会像您当前的代码那样成为一体的魔法。

There may be more bugs that I haven't noticed, but that should definitely get you on your way to fixing the problem.可能还有更多我没有注意到的错误,但这绝对可以帮助您解决问题。

ero1ca's answer explains many of the issues. ero1ca 的回答解释了许多问题。

As for the last part of the code not working, that is because you loop through every char in the password, and increment based on what that character is.至于代码的最后一部分不起作用,那是因为您遍历密码中的每个字符,并根据该字符的内容递增。 With a mandated length > 3, your conditional statements at the end wont cover the password!如果强制长度大于 3,您最后的条件语句将不会覆盖密码!

below is a revised version of the code.下面是代码的修订版。

password = input("Please enter a password between 6 - 12 characters long: ")

while not len(password) in range(6, 13):
    if len(password) < 6:
        print ("Password too low!")
    else:
        print ("Password to high!")
    password = input("Please enter a password between 6 - 12 characters long: ")

Upper = 0
Lower = 0
Numerical = 0

if any(x.isupper() for x in password):
    Upper = 1
if any(x.islower() for x in password):
    Lower = 1
if any(x.isdigit() for x in password):
    Numerical = 1

Strength =int(Upper + Lower + Numerical)
if Strength == 1:
    print ("Password is weak!")
elif Strength == 2:
    print ("Password is medium!")
else:
    print ("Password is strong!")

Use raw_input instead of input() for python 2.7在 python 2.7 中使用 raw_input 而不是 input()

Edit Holy Necro!编辑神圣的死灵! My mistake lads!我的错误小伙子们!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM