【问题标题】:Can someone please fix it so that the function can detect whether the input has at least one symbol in it?有人可以修复它,以便该函数可以检测输入中是否至少包含一个符号?
【发布时间】:2022-01-20 00:54:26
【问题描述】:
import os
import time

def strong(password, verifier):
  symbols = "! # $ % & ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~"
  password = str(password)
  if len(password) > 8:
    if len(password) < 15:
        for i in symbols:
          if password.find(symbols) is True:

我想看看我是否可以修复这部分,因为我不确定如何使用此功能查找密码中的特殊字符。

            if password.isalnum():
              verifier = 1
              return 'good password', verifier
            elif password.isalpha():
              return 'Your password is only letters'
            elif password.isnumeric():
              return 'Your password is only numbers'
          else:
            return 'no symbols'
    else:
      return 'Too big'
  else:
    return 'Too small'

【问题讨论】:

标签: python list function parsing special-characters


【解决方案1】:

您可以使用any() 和生成器理解来检查:

if any(ch in password for ch in symbols):

我会指出,符号列表也包含空格,因此如果您不希望将空格作为符号,则可能需要将其删除。

【讨论】:

  • symbols.split() 可用于删除空格
【解决方案2】:

您可以通过在每次检查失败后只使用returning 来简化此操作(并使调试更容易),而不是使用所有深层嵌套的if/else。由于在“好密码”情况下,您将返回“验证者”以及“好密码”消息,我想您可能也希望为“错误密码”情况返回错误值:

def is_strong(password):
    symbols = set("!#$%&()*+,-./:;=?@[]^_`{|}~")
    if len(password) < 9:
        return "Too small", False
    if len(password) > 14:
        return "Too big", False
    if not any(c in symbols for c in password):
        return "No symbols", False
    if password.isalpha():
        return "Your password is only letters", False
    if password.isnumeric():
        return "your password is only numbers", False
    return "Good password", True

一旦函数被简化,我们可以看到isalphaisnumeric 检查实际上不会做任何事情,因为我们已经确定密码包含非字母数字符号!相反,我认为您想要这样做:

def is_strong(password):
    symbols = set("!#$%&()*+,-./:;=?@[]^_`{|}~")
    if len(password) < 9:
        return "Too small", False
    if len(password) > 14:
        return "Too big", False
    if not any(c in symbols for c in password):
        return "No symbols", False
    if not any(c.isalpha() for c in password):
        return "No letters", False
    if not any(c.isdigit() for c in password):
        return "No numbers", False
    return "Good password", True

我们可以这样测试函数:

strong, password = False, ""
while not strong:
    password = input("Set password: ")
    msg, strong = is_strong(password)
    print(msg)
Set password: bob
Too small
Set password: bobledeboingerberoo
Too big
Set password: bobbington
No symbols
Set password: bobbington!
No numbers
Set password: bobbington3!
Good password

【讨论】:

  • 谢谢!这是一个很好的解决方案。
【解决方案3】:

将您的特殊字符列表设置为一组,您将能够使用isdisjoint 函数检查密码中是否没有符号:

if {*"!#$%&()*+,-./:;=?@[]^_`{|}~"}.isdisjoint(password):
    print("Password has no symbols")
else:
    print("Password contains at least one symbol")

  

【讨论】:

    猜你喜欢
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-08
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多