【问题标题】:How would I check how many numbers are in a string in python?(Password strength checker) [duplicate]我如何检查python中的字符串中有多少个数字?(密码强度检查器)[重复]
【发布时间】:2016-06-04 19:18:59
【问题描述】:

我正在做一个密码强度检查器作为一个学校项目,但我正在努力寻找一种方法来检查密码中有多少个整数,因此我正在检查三件事的密码,但我就是不知道如何检查整数。如果你们可以帮助我的解决方案可以包括更好的 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

【问题讨论】:

  • 计算密码强度的更好方法是使用熵:xkcd.com/936
  • 您是否尝试过将字符串拆分为字符并以这种方式计算数字的出现次数?您不需要以这种方式使用正则表达式。
  • 不,我很抱歉我不会碰巧知道。
  • 那里可能有一个图书馆可以为你做这件事,我会去寻找它,但正如我所说的那样,它是给学校的,我必须编写代码。
  • 您问题中的代码没有正确缩进

标签: python


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2012-06-12
    • 2012-10-05
    • 2014-10-26
    • 2016-06-16
    • 2017-12-31
    • 1970-01-01
    • 2018-05-19
    • 2016-12-04
    • 1970-01-01
    相关资源
    最近更新 更多