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

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


要求是密码长度至少需要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

在我的测试运行返回多行“无大写”和“无小写”后,我进行了一些更改并使用了 .isalnum 运算符。


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


慕婉清6462132
浏览 168回答 4
4回答

拉风的咖菲猫

没有正则表达式你可以使用any和str.isnumericif&nbsp;not&nbsp;any(map(str.isnumeric,&nbsp;password): &nbsp;&nbsp;&nbsp;&nbsp;print('No&nbsp;numbers')

小唯快跑啊

许多 python 在线密码检查器,例如:https ://www.geeksforgeeks.org/password-validation-in-python/这是一个快速的控制台程序,您可以像这样调用:$ python3 password_checker.py "Testf7788790##$"Testing password:&nbsp; Testf7788790##$Password is valid:&nbsp; True$ python3 password_checker.py "insecurePassword"Testing password:&nbsp; insecurePasswordPassword should contain at least one numberPassword is valid:&nbsp; False的内容password_checker.py:#!/usr/bin/pythonimport sysdef password_check(passwd):&nbsp; &nbsp; symbols = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '+', '=']&nbsp; &nbsp; isValid = False&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if len(passwd) < 10:&nbsp; &nbsp; &nbsp; &nbsp; print('Password should be at least 10 characters')&nbsp; &nbsp; elif not any(char.isdigit() for char in passwd):&nbsp; &nbsp; &nbsp; &nbsp; print('Password should contain at least one number')&nbsp; &nbsp; elif not any(char.isupper() for char in passwd):&nbsp; &nbsp; &nbsp; &nbsp; print('Password should contain at least one uppercase character')&nbsp; &nbsp; elif not any(char.islower() for char in passwd):&nbsp; &nbsp; &nbsp; &nbsp; print('Password should contain at least one lowercase character')&nbsp; &nbsp; elif not any(char in symbols for char in passwd):&nbsp; &nbsp; &nbsp; &nbsp; print('Password should contain at least one special character from list: ', symbols)&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; isValid = True&nbsp; &nbsp; return isValidarguments = sys.argvif len(arguments) < 2:&nbsp; &nbsp; print('No password could be parsed by argv')&nbsp; &nbsp; valid_password = Falseelse:&nbsp; &nbsp; password = arguments[1]&nbsp; &nbsp; print('Testing password: ', password)&nbsp; &nbsp; valid_password = password_check(password)print('Password is valid: ', valid_password)

互换的青春

(其他答案已经给了你很好的答案,所以这不是一个完整的答案,它只是对出了什么问题的解释。)导致问题的代码区域是for x in password:&nbsp; &nbsp; &nbsp; &nbsp; if x.isupper():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pass&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('no upper case')for y in password:&nbsp; &nbsp; &nbsp; &nbsp; if y.islower():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pass&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('no lower case')您正在遍历整个密码,检查每个字符是否为大写,如果不是,则打印出“无大写”。问题是如果单词的单个字符不是大写,"that_character_that_isn't_uppercase".isupper() 将返回 false,并打印错误语句。例如,密码 PaSSWORD 将返回一个“无大写字母”,因为“a”.isupper() 为 False。密码 passworD 将返回 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():&nbsp; &nbsp; print("No upper case")elif password.isupper():&nbsp; &nbsp; print("No lower case")#Again, password.isupper() sees if all letters are uppercase(which means that there is no lowercase letters).希望这有帮助!

翻过高山走不出你

我宁愿使用正则表达式。In [122]: def validate(password):&nbsp; &nbsp; &nbsp;...:&nbsp; &nbsp; &nbsp;return True if re.match("(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}", password) else False&nbsp; &nbsp; &nbsp;...:In [123]: validate("helloas")Out[123]: FalseIn [124]: validate("helH12asfgvGh")Out[124]: True
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python