【问题标题】:How to escape Iterated loop Python如何逃避迭代循环Python
【发布时间】:2021-05-01 22:36:11
【问题描述】:

你好,

我最近开始学习 python,由于我对 JavaScript 有所了解,所以我对 python 有了一点领先优势,这是大多数初学者在学习 python 时所拥有的。自从我开始学习 python 以来,我一直在接受挑战,同时也在做自己的小项目。我现在面临的挑战是制作一个密码验证器。

我的问题:

我花了 2 个多小时试图弄清楚如何计算字符串(密码)中特殊字符和数字的数量。 我还没有完善计数,但我现在的主要问题是获取打印结果的功能

好的,我这里有柜台:

def password_validator():
    password = input("Please input your password >>> ")

    for element in password:

        num_list = ['1','2','3','4','5','6','7','8','9','0',]

        for x in num_list:
            num_count = 0
            if element.count(x):
                num_count = 1
                if element.count(x):
                    num_count = 2
                    num_pass_strength = 1
                    if num_count == 2:
                        num_pass_strength = 1
                else:
                    num_pass_strength = 0
            elif num_count == 0:
                num_pass_strength = 0

        spec_chars = ['!', '@', '#', '$', '%', '&', '*',]
        
        for c in spec_chars:
            spec_count = 0
            if element.count(c):
                spec_count = 1
                if element.count(c):
                    spec_count = 2
                    if spec_count == 2:
                        spec_pass_strength = 1
                else:
                    spec_pass_strength = 0
            elif spec_count == 0:
                spec_pass_strength = 0

这基本上计算密码中的数字和特殊字符的数量。

这是我计算密码强度的方法:

        add_strength = 0

        if num_pass_strength == 1:
            add_strength += 1
        if spec_pass_strength == 1:
            add_strength +=1

        pass_strength = add_strength

这为 add_strength 变量增加了一个强度,pass_strength 也从 add_strength 变量中获取了它的数据。

这就是我认为我遇到的问题:

            if pass_strength == 1:
                is_weak_pass = True
            elif pass_strength == 2:
                is_medium_pass = True
            elif pass_strength == 3:
                is_strong_pass = True

        if is_weak_pass == True:
            print("Weak")
        elif is_medium_pass == True:
            print("Medium")
        elif is_strong_pass == True:
            print("Strong")

这应该检查强度的值并为所需的输出定义一个布尔变量。

我现在的主要问题是,当我运行脚本并输入密码时,
它给我留下了这样的结果

Output:
Please input your password >>> &^%&^%
Weak
Weak
Weak
Weak
Weak
Weak

谁能告诉我我做错了什么。这真的让我很烦,我什至很想为我的另一个项目找出类似的东西。谢谢

完整代码:


def password_validator():
    password = input("Please input your password >>> ")

    for element in password:

        num_list = ['1','2','3','4','5','6','7','8','9','0',]

        for x in num_list:
            num_count = 0
            if element.count(x):
                num_count = 1
                if element.count(x):
                    num_count = 2
                    num_pass_strength = 1
                    if num_count == 2:
                        num_pass_strength = 1
                else:
                    num_pass_strength = 0
            elif num_count == 0:
                num_pass_strength = 0

        spec_chars = ['!', '@', '#', '$', '%', '&', '*',]
        
        for c in spec_chars:
            spec_count = 0
            if element.count(c):
                spec_count = 1
                if element.count(c):
                    spec_count = 2
                    if spec_count == 2:
                        spec_pass_strength = 1
                else:
                    spec_pass_strength = 0
            elif spec_count == 0:
                spec_pass_strength = 0

            add_strength = 0

            if num_pass_strength == 1:
                add_strength += 1
            if spec_pass_strength == 1:
                add_strength +=1

            pass_strength = add_strength
  
            if pass_strength == 1:
                is_weak_pass = True
            elif pass_strength == 2:
                is_medium_pass = True
            elif pass_strength == 3:
                is_strong_pass = True

        if is_weak_pass == True:
            print("Weak")
        elif is_medium_pass == True:
            print("Medium")
        elif is_strong_pass == True:
            print("Strong")


password_validator()

【问题讨论】:

  • 使用调试器查看何时将这些is_ 变量设置为True
  • 代码中有很多冗余,清理它可能有助于暴露错误。

标签: python arrays loops validation iteration


【解决方案1】:

据我所见,num_pass_strength 和 spec_pass_strength 尚未初始化,每次 for 循环迭代时都会为这两个变量分配新值。

def password_validator():
    password = input("Please input your password >>> ")

    for element in password:

        num_list = ['1','2','3','4','5','6','7','8','9','0',]

        for x in num_list:
            num_count = 0
            if element.count(x):
                num_count = 1
                if element.count(x):
                    num_count = 2
                    num_pass_strength = 1
                    if num_count == 2:
                        num_pass_strength = 1
                else:
                    num_pass_strength = 0 # this statement resets num_pass_strength whenever element is not in num_list
            elif num_count == 0:
                num_pass_strength = 0 # this statement resets num_pass_strength whenever element is not in num_list

        spec_chars = ['!', '@', '#', '$', '%', '&', '*',]
        
        for c in spec_chars:
            spec_count = 0
            if element.count(c):
                spec_count = 1
                if element.count(c):
                    spec_count = 2
                    if spec_count == 2:
                        spec_pass_strength = 1
                else:
                    spec_pass_strength = 0 # this statement resets spec_pass_strength whenever element is not in spec_char
            elif spec_count == 0:
                spec_pass_strength = 0 # this statement resets spec_pass_strength whenever element is not in spec_char

我建议简化代码以检查元素是数字还是特殊字符。

# Initialize your variables
num_pass_strength = 0
spec_pass_strength = 0
for element in password:

        num_list = ['1','2','3','4','5','6','7','8','9','0',]

        # check whether element is in num_list
        if element in num_list:
            num_pass_strength = 1
                
        spec_chars = ['!', '@', '#', '$', '%', '&', '*',]
        
        # check whether element is in spec_chars
        if element in spec_chars:
            spec_pass_strength = 1

附带说明,如果您要计算 Python 字符串中的字符数,也可以考虑使用 collections.Counter

【讨论】:

    【解决方案2】:

    请参阅下面的工作版本,了解您正在尝试做的事情,建立在您最初的尝试之上。我认为对您有好处的做法是仔细研究解决方案,看看它有何不同以及您的尝试失败的原因。您尝试遇到的一些问题:

    • is_weak_pass 和其他 is_ 变量试图在设置之前被调用。例如,如果 PW 是中等的,那么 is_weak_pass 永远不会被设置,并且会出现错误,因为 is_weak_pass == True 行将失败
    • 元素的循环嵌套太高,您需要在进入后续阶段之前完成元素的循环,在您的示例中,每个元素都会发生打印阶段
    • 你有代码块:
    if element.count(c):
     spec_count = 1
     if element.count(c):
      spec_count = 2
    

    这没有意义,因为如果外部 if 块通过,内部 if 块将始终通过

    • 您在循环内部设置了变量,而这些变量应该在循环外部设置为计数器,即 num_pass_strength 需要在元素循环外部设置

    无论如何,下面的解决方案并不是唯一的方法,但希望是您遇到的一些问题以及如何解决这些问题的示例

    
    def password_validator(password):
      
        num_list = ['1','2','3','4','5','6','7','8','9','0',]
        spec_chars = ['!', '@', '#', '$', '%', '&', '*',]
        num_count = 0
        spec_count = 0
        for element in password:    
            
            for x in num_list:
                if element.count(x):
                    num_count += 1
            
            for c in spec_chars:    
                if element.count(c):
                    spec_count += 1
    
    
        num_pass_strength = num_count > 1
    
        spec_pass_strength = spec_count > 1
    
        pass_strength = 0
    
        if num_pass_strength:
            pass_strength += 1
        if spec_pass_strength:
            pass_strength +=1
    
        if pass_strength == 0:
            print("Weak")
        elif pass_strength == 1:
            print("Medium")
        elif pass_strength == 2:
            print("Strong")        
    
    pw = '&^%&^%'
    print(pw)
    password_validator(pw)
    
    pw = '1as'
    print(pw)
    password_validator(pw)
    
    pw = '11234as'
    print(pw)
    password_validator(pw)
    
    
    pw = 'asp'
    print(pw)
    password_validator(pw)
    
    pw = '1$325^#beasp'
    print(pw)
    password_validator(pw)
    

    【讨论】:

      【解决方案3】:

      在多个循环中存在大量冗余和变量重新分配,这使您的代码有点难以阅读(至少对我而言)。

      几点开始:

      if element.count(x):
          num_count = 2
          num_pass_strength = 1
          if num_count == 2:
              num_pass_strength = 1
      

      你在这里定义num_count = 2并定义num_pass_strength = 1,然后在运行测试后立即看到if num_count == 2才再次定义num_pass_strength = 1

      您在整个代码中都有类似的逻辑行。

      这个块可以通过两种方式简化,具体取决于你想要做什么:

      if pass_strength == 1:
          is_weak_pass = True
      elif pass_strength == 2:
          is_medium_pass = True
      elif pass_strength == 3:
          is_strong_pass = True
      
      if is_weak_pass == True:
          print("Weak")
      elif is_medium_pass == True:
          print("Medium")
      elif is_strong_pass == True:
          print("Strong")
      

      首先,不要将is_*_pass 定义为布尔值,只测试一次,您可以将打印语句移到if 语句中(我将在下面显示)。其次,作为一般的python语法提示,如果测试定义为布尔值的变量,则不必使用if variable == True:if variable:将返回变量的布尔值。

      要记住的另一件事,尤其是在使用开放式变量时(即密码中的数字/特殊字符可以少于一个或多于三个),是使用<=>= 来确保这些考虑到极端情况。

      这是您的代码的编辑版本,它消除了冗余,但(我认为)保留了您的总体目标。还有其他可能更有效的方法可以做到这一点,这只是在考虑类似风格的情况下重新编写代码。将num_listspec_chars 从字符串列表更改为字符串只是一种样式选择,对函数的结果没有影响。

      如果您想分别根据数字计数和特殊字符计数来确定密码的强度,可以使用不同的和/或语句进行编辑。

      def password_validator():
          password = input("Please input your password >>> ")
      
          num_list = '1234567890'
          spec_chars = '!@#$%&*'
          num_count = 0
          spec_count = 0
          for element in password:
              if element in num_list:
                  num_count += 1
              if element in spec_chars:
                  spec_count += 1
      
          pass_strength = num_count + spec_count
      
          if pass_strength <= 1:
              print("Weak")
          elif pass_strength == 2:
              print("Medium")
          elif pass_strength >= 3:
              print("Strong")
      

      【讨论】:

        猜你喜欢
        • 2011-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-19
        • 2012-06-27
        • 2023-03-05
        • 1970-01-01
        相关资源
        最近更新 更多