【问题标题】:How do I compare an input to strings in a list in Python?如何将输入与 Python 列表中的字符串进行比较?
【发布时间】:2021-07-12 19:05:45
【问题描述】:

我想创建一个密码程序来检查输入中是否有特殊字符。

所以我创建了一个包含一些特殊字符的列表,这样如果在输入中只检测到一个或多个符号,那么它应该做一些事情:

SpecialSymbols =['$', '@', '#', '%']
Password = input("type password")

if Password in SpecialSymbols:
    print("strong password")
else:
    print("no special character detected")

我认为我还需要使用 for 循环,在这种情况下它会打印所有项目。或者我可能只需要检查特定字符而不是整个输入,我该怎么做?

【问题讨论】:

  • if all(x in SpecialSymbols for x in Password): print("strong password")?或if any(x in SpecialSymbols for x in Password): print("strong password")?
  • 你的意思是if SpecialSymbols in Password:
  • strong = False for symbol in SpecialSymbols:if symbol in Password:strong=True if strong:print(strong password) - 很难在一行中打印多行,如果您不清楚,请告诉我。

标签: python string list char passwords


【解决方案1】:

此代码检测密码中有多少特殊字符。

SpecialSymbols =['$', '@', '#', '%']
Password = input("type password")

number = 0
for symbol in SpecialSymbols: # for each symbol
    if symbol in Password: # check if it is in the password
        number += 1

if number >= 1: # symbol detected in the password
    print("strong password (%d special symbols in)" % number)
else:
    print("no special character detected")

更简洁:只检查是否有任何符号,不使用出现计数器,而是使用布尔值strong 值:

strong = False
for symbol in SpecialSymbols:
    if symbol in Password:
        strong = True
        break

if strong:
    print("strong password")
else:
    print("no special character detected")

【讨论】:

    【解决方案2】:

    您可以使用any(如果列表中应该至少有一个特殊字符)或all(如果输入字符串中的列表中必须有所有特殊字符)。

    if any(x in SpecialSymbols for x in Password):
    if all(x in SpecialSymbols for x in Password):
    

    a Python demo:

    SpecialSymbols =['$', '@', '#', '%']
    Passwords = ["String #$%@", "String #1", "String"]
    for Password in Passwords:
        if any(x in SpecialSymbols for x in Password):
            print("strong password")
        else:
            print("no special character detected")
    

    此 sn-p 的输出:

    strong password
    strong password
    no special character detected
    

    【讨论】:

      【解决方案3】:

      遍历字符串

      SpecialSymbols =['$', '@', '#', '%']
      Password = input("type password")
      strong = False
      for c in Password:
          if c in SpecialSymbols:
              strong = True
              break
      if strong:
          print("Password is strong")
      else:
          print('password is weak')
      

      【讨论】:

        【解决方案4】:

        为了解决您的问题,我创建了一个函数来检查单词中的每个字母是否是特殊字符。如果是,那么它将高于score 并返回true。然后就可以用这个函数来输入密码了。

        代码如下:

        SpecialSymbols =['$', '@', '#', '%']
        Password = input("type password")
        
        def strongPassword(password):
            score = 0
            for letter in Password:
                for spec in SpecialSymbols:
                    if letter == spec :
                        score = score + 1
            print(score)
            if score : 
                return True
        
        strongPassword(Password)
        
        if strongPassword(Password):
            print("strong password")
        else:
            print("no special character detected")
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-12-18
          • 1970-01-01
          相关资源
          最近更新 更多