我如何检查python中字符串中有多少个数字?(密码强度检查器)

时间:2016-06-03 09:06:38

标签: python

我正在制作一个密码强度检查器作为学校项目,但我很难找到一种方法来检查密码中有多少整数,因此我在三个方面检查密码,但我无法弄清楚如何检查整数。如果你们可以帮助我的解决方案可以包括更好的re库。谢谢!

到目前为止,这是我的问题代码。

    import re

    i_pwdStrength = i_pwdLength + i_pwdChar + i_pwdInt
    i_pwdLength = 0
    i_pwdChar = 0
    i_pwdInt = 0
    i_attempts = 0
    numbAttemps = int(input("Enter the amount of passwords you would like to check \n(MUST BE A NUMBER)"))
    pwd = input("Enter Password")


    while numbAttempts < i_attempts
        pwd = pwd.lower()

    i_pwdLength = pwd.length
    if i_pwdLength <= 3:
        i_pwdLength = 1
    elif i_pwdLength > 3 && <= 6:
        i_pwdLength = 2
    elif i_pwdLength > 6 && <= 9:
        i_pwdLength = 3
    else i_pwdLength > 9:
        i_pwdLength = 4

    i_pwdChar =      #This is a very long way of writing this code as I could not find a way to compress these lines. I may have used the 'or' command but I could not
    if re.search(r'[x]',pwd):              # work out how to integrate it and i thought that this code looked neater
        i_pwdChar = 1
    elif re.search(r'[w]'pwd):
        i_pwdChar = 1
    elif re.search(r'[y]'pwd):
        i_pwdChar = 1
    elif re.search(r'[z]'pwd):
        i_pwdChar = 1
    elif re.search(r'[x]',pwd) and re.search(r'[w]',pwd):
        i_pwdChar = 2
    elif re.search(r'[x]',pwd) and re.search(r'[y]',pwd):
        i_pwdChar = 2
    elif re.search(r'[x]',pwd) and re.search(r'[z]',pwd):
        i_pwdChar = 2
    elif re.search(r'[w]',pwd) and re.search(r'[y]',pwd):
        i_pwdChar = 2
    elif re.search(r'[w]',pwd) and re.search(r'[z]',pwd):
        i_pwdChar = 2
    elif re.search(r'[y]',pwd) and re.search(r'[z]',pwd):
        i_pwdChar = 2
    elif re.search(r'[w]',pwd) and re.search(r'[x]',pwd) and re.search(r'[y]',pwd):
        i_pwdChar = 3
    elif re.search(r'[z]',pwd) and re.search(r'[x]',pwd) and re.search(r'[y]',pwd):
        i_pwdChar = 3
    elif re.search(r'[w]',pwd) and re.search(r'[z]',pwd) and re.search(r'[y]',pwd):
        i_pwdChar = 3
    else re.search(r'[w]',pwd) and re.search(r'[x]',pwd) and re.search(r'[y]',pwd) and re.search(r'[z]',pwd)
        i_pwdChar = 4

由于我收到的所有帮助,我已经完成了代码。

    import re

    i_numbAttempts = int(input("How many passwords do you want to try?\nMUST BE A NUMBER!")) # Determines how many times the while loop further down
    attempts = 0    # sets the variable attempts to 0 to ensure that the while loop works                # repeats itself

    while i_numbAttempts > attempts:      # while the passwords entered is more than attempts:
        pwd = input("Enter Password: ")   # user enters password to check
        pwd = pwd.lower()                 # sets the password to lowercase (QjwtwWyeRvgTRDU would become qjwtwwyervgtrdu)

        if len(pwd) == 0:                        # if the user does not enter a password, it outputs: You must ypre something!
            print("You must type something!")
            i_pwdLength = 0
        elif len(pwd) >= 1 and len(pwd) <= 3:    # if the length is a certain length,
            i_pwdLength = 1                      # it will assign a corresponding value to the i_pwdLength variable
        elif len(pwd) > 3 and len(pwd) <= 6:     
            i_pwdLength = 2
        elif len(pwd) > 6 and len(pwd) <= 9:
            i_pwdLength = 3
        elif len(pwd) > 9:
            i_pwdLength = 4

        if re.search(r'[w]',pwd) and re.search(r'[x]',pwd) and re.search(r'[y]',pwd) and re.search(r'[z]',pwd):
            i_pwdChar = 4
        elif re.search(r'[w]',pwd) and re.search(r'[x]',pwd) and re.search(r'[y]',pwd):
            i_pwdChar = 3
        elif re.search(r'[z]',pwd) and re.search(r'[x]',pwd) and re.search(r'[y]',pwd):
            i_pwdChar = 3
        elif re.search(r'[w]',pwd) and re.search(r'[z]',pwd) and re.search(r'[y]',pwd):
            i_pwdChar = 3
        elif re.search(r'[w]',pwd) and re.search(r'[x]',pwd) and re.search(r'[y]',pwd) and re.search(r'[z]',pwd):
            i_pwdChar = 4
        elif re.search(r'[x]',pwd) and re.search(r'[w]',pwd):              # This section checks the password for any 
            i_pwdChar = 2
        elif re.search(r'[x]',pwd) and re.search(r'[y]',pwd):               # Combination of the letters w,x,y and z.
            i_pwdChar = 2
        elif re.search(r'[x]',pwd) and re.search(r'[z]',pwd):               # I could not find a way to compress these
            i_pwdChar = 2
        elif re.search(r'[w]',pwd) and re.search(r'[y]',pwd):               # Lines so i left them as is as i thought
            i_pwdChar = 2
        elif re.search(r'[w]',pwd) and re.search(r'[z]',pwd):               # That it looked neater
            i_pwdChar = 2
        elif re.search(r'[y]',pwd) and re.search(r'[z]',pwd):
            i_pwdChar = 2
        elif re.search(r'[x]',pwd):
            i_pwdChar = 1
        elif re.search(r'[w]',pwd):
            i_pwdChar = 1
        elif re.search(r'[y]',pwd):
            i_pwdChar = 1
        elif re.search(r'[z]',pwd):
            i_pwdChar = 1
        else:
            i_pwdChar = 0

        if (len([l for l in pwd if l.isdigit()])) == 0:                                                     # This code checks how many integers are included
            i_pwdInt = 0
        elif (len([l for l in pwd if l.isdigit()])) >= 1 and (len([l for l in pwd if l.isdigit()])) < 3:    # In the password, and assigns a number to the 
            i_pwdInt = 1
        elif (len([l for l in pwd if l.isdigit()])) >= 3 and (len([l for l in pwd if l.isdigit()])) < 5:    # Variable i_pwdInt accordingly
            i_pwdInt = 2
        elif (len([l for l in pwd if l.isdigit()])) >=5 and (len([l for l in pwd if l.isdigit()])) <7:
            i_pwdInt = 3
        elif (len([l for l in pwd if l.isdigit()])) >=7:
            i_pwdInt = 4

        i_pwdStrength = i_pwdLength + i_pwdChar + i_pwdInt     # this determines the passwords overall strength

        if i_pwdStrength > 0 and i_pwdStrength <= 4 :
            print("This password is bad")
        elif i_pwdStrength > 4 and i_pwdStrength <= 8:
            print("This password is OK")
        elif i_pwdStrength > 8 and i_pwdStrength <= 11:
            print("this password is good")
        elif i_pwdStrength == 12:
            print("this password is the best!!")

        attempts = attempts + 1

1 个答案:

答案 0 :(得分:4)

我认为你应该使用isdigit方法。以下命令将返回密码中的位数。

len([l for l in pwd if l.isdigit()])