有人可以修复它以便该函数可以检测输入中是否至少有一个符号吗?
Can someone please fix it so that the function can detect whether the input has at least one symbol in it?
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'
您可以使用 any()
和生成器理解来检查:
if any(ch in password for ch in symbols):
我要指出的是,符号列表还包含 space,因此如果您不希望 space 符合符号条件,则可能需要将其删除。
您可以通过在每次检查失败后 return
进行简化(并使调试更容易),而不是让所有深层嵌套 if/else
。因为在“好密码”的情况下,你 return 是一个“验证者”以及“好密码”消息,我想你可能想 return 为“密码错误”案例:
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
简化函数后,我们可以看到 isalpha
和 isnumeric
检查实际上不会做任何事情,因为我们已经确定密码包含非字母数字符号!相反,我认为您想这样做:
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
将您的特殊字符列表设为一组,您将能够使用 isdisjoint
函数来检查密码中是否没有符号:
if {*"!#$%&()*+,-./:;=?@[]^_`{|}~"}.isdisjoint(password):
print("Password has no symbols")
else:
print("Password contains at least one symbol")
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'
您可以使用 any()
和生成器理解来检查:
if any(ch in password for ch in symbols):
我要指出的是,符号列表还包含 space,因此如果您不希望 space 符合符号条件,则可能需要将其删除。
您可以通过在每次检查失败后 return
进行简化(并使调试更容易),而不是让所有深层嵌套 if/else
。因为在“好密码”的情况下,你 return 是一个“验证者”以及“好密码”消息,我想你可能想 return 为“密码错误”案例:
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
简化函数后,我们可以看到 isalpha
和 isnumeric
检查实际上不会做任何事情,因为我们已经确定密码包含非字母数字符号!相反,我认为您想这样做:
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
将您的特殊字符列表设为一组,您将能够使用 isdisjoint
函数来检查密码中是否没有符号:
if {*"!#$%&()*+,-./:;=?@[]^_`{|}~"}.isdisjoint(password):
print("Password has no symbols")
else:
print("Password contains at least one symbol")