Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何编写python函数来检查用户输入的密码是否不强?_Python_Python 3.x_String - Fatal编程技术网

如何编写python函数来检查用户输入的密码是否不强?

如何编写python函数来检查用户输入的密码是否不强?,python,python-3.x,string,Python,Python 3.x,String,我需要为一个任务编写一个代码,该任务将接受用户输入的密码(作为字符串),并让用户知道输入的哪些元素使密码变弱 要求密码长度至少为8个字符,包括大小写字母和数字。我的代码不需要确定密码是否强,只需要确定密码弱时为什么弱 到目前为止,我编写的代码如下: size = len(password) if size < 8: print('not long enough') if password.isalnum(): pa

我需要为一个任务编写一个代码,该任务将接受用户输入的密码(作为字符串),并让用户知道输入的哪些元素使密码变弱

要求密码长度至少为8个字符,包括大小写字母和数字。我的代码不需要确定密码是否强,只需要确定密码弱时为什么弱

到目前为止,我编写的代码如下:

    size = len(password)
    
    if size < 8:
        print('not long enough')
    
    if password.isalnum():
        pass
    else:

    for x in password:
        if x.isupper():
            pass
        else:
            print('no upper case')
    
    for y in password:
        if y.islower():
            pass
        else:
            print('no lower case')
            
    return
size=len(密码)
如果尺寸小于8:
打印('不够长')
如果密码为.isalnum():
通过
其他:
对于密码中的x:
如果x.isupper():
通过
其他:
打印('无大写')
对于密码中的y:
如果y.islower():
通过
其他:
打印('无小写')
回来
在测试运行返回多行“无大写”和“无小写”后,我做了一些更改并使用了.isalnum操作符


如果有人能把我推向正确的方向,我将不胜感激,因为这让我现在有点困惑

没有正则表达式,您可以使用
any
str.isnumeric

if not any(map(str.isnumeric, password):
    print('No numbers')

我宁愿用正则表达式

In [122]: def validate(password):
     ...:     return True if re.match("(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}", password) else False
     ...:

In [123]: validate("helloas")
Out[123]: False

In [124]: validate("helH12asfgvGh")
Out[124]: True
(其他答案已经给了你很好的答案,所以这不是一个完整的答案,它只是一个错误的解释。)导致问题的代码区域是

for x in password:
        if x.isupper():
            pass
        else:
            print('no upper case')
for y in password:
        if y.islower():
            pass
        else:
            print('no lower case')
您正在循环检查整个密码,检查每个字符是否为大写,如果不是,则打印“无大写”。问题是,如果单词的单个字符不是大写,“that_character_that_'t't_uppercase.”isupper()将返回false,并打印错误语句。例如,密码将返回一个“no upper case”,因为“a”.isupper()为False。密码将返回7个“无大写字母”,因为p、a、s、s、w、o、r等字符都是小写字母。同样的事情也发生在x.islower()测试中,您将看到每个字符是否都是小写的。我将实现如下内容:

#password.islower() will return true if all the entire string is lowercase(and thus not uppercase)
if password.islower():
    print("No upper case")
elif password.isupper():
    print("No lower case")
#Again, password.isupper() sees if all letters are uppercase(which means that there is no lowercase letters).

希望这有帮助

许多python密码检查器在线,例如:

下面是一个可以调用的快速控制台程序,如:

$ python3 password_checker.py "Testf7788790##$"
Testing password:  Testf7788790##$
Password is valid:  True
$ python3 password_checker.py "insecurePassword"
Testing password:  insecurePassword
Password should contain at least one number
Password is valid:  False
密码检查器.py的内容

#!/usr/bin/python

import sys

def password_check(passwd):
    symbols = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '+', '=']
    isValid = False
      
    if len(passwd) < 10:
        print('Password should be at least 10 characters')
    elif not any(char.isdigit() for char in passwd):
        print('Password should contain at least one number')
    elif not any(char.isupper() for char in passwd):
        print('Password should contain at least one uppercase character')
    elif not any(char.islower() for char in passwd):
        print('Password should contain at least one lowercase character')
    elif not any(char in symbols for char in passwd):
        print('Password should contain at least one special character from list: ', symbols)
    else:
      isValid = True

    return isValid

arguments = sys.argv
if len(arguments) < 2:
    print('No password could be parsed by argv')
    valid_password = False
else:
    password = arguments[1]
    print('Testing password: ', password)
    valid_password = password_check(password)

print('Password is valid: ', valid_password)
#/usr/bin/python
导入系统
def密码检查(passwd):
符号=['!'、'@'、'#'、'$'、'%'、'^'、'&'、'*'、'('、')'、'-'、'#'、'+'、'=']
isValid=False
如果len(passwd)<10:
打印('密码至少应为10个字符')
elif not any(passwd中char的char.isdigit()):
打印('密码应至少包含一个数字')
elif not any(passwd中char的char.isupper()表示):
打印('密码应至少包含一个大写字符')
elif not any(对于passwd中的char,char.islower()为):
print('密码应至少包含一个小写字符')
elif not any(符号中的字符表示密码中的字符):
打印('密码应至少包含列表中的一个特殊字符:',符号)
其他:
isValid=True
返回有效
参数=sys.argv
如果len(参数)<2:
打印('argv'无法解析密码')
有效密码=False
其他:
密码=参数[1]
打印('测试密码:',密码)
有效密码=密码检查(密码)
打印('密码有效:',有效\u密码)

我建议使用正则表达式?不需要特殊字符?您的经验有什么问题?我建议您在这里检查这个问题。您需要在第一个
else
语句之后修复缩进。(这不是问题的直接原因,只是一个补充意见。)感谢您的回复!最后,我使用了“if not any(char.isupper()表示密码中的char)”,但对每个条件使用了单独的if语句,而不是elif语句。我通过assignment tester运行了代码,结果成功:)