如何替换双while循环?

2024-06-16 12:49:05 发布

您现在位置:Python中文网/ 问答频道 /正文

评估密码强度的程序首先检查密码长度是否正确(6-12),以及密码是否包含扩展ASCII字符,即127-255。你知道吗

我遇到的问题是,如果用户首先输入的密码太短或太长,那么程序就不会在用户输入的下一个密码中检查扩展字符,反之亦然,而是先检查扩展字符。你知道吗

我实现了一个双while循环,如果这是正确的调用方式,但是它仍然没有带来预期的效果。你知道吗

import re
import time
#score variable, responsible for later defining if passwords is weak, medium or strong
Score = 0
#list for improvements to password printed out with score at the end
Improvements = []
Password = ""
#defines function for checking for presence of extended characters (i.e. ascii 128-255)
def hasExtended(s):
    return any(ord(i) > 127 for i in s)
#inputs password
Password = input("Please enter a password:")
Password_length = len(Password)
Extended_presence = hasExtended(Password)
#checks password length (6-12), asks user to re-input until password is within boundaries
#checks if password contains extended characters
#double while loops to allow for two conditions simultaneously
while Password_length < 6 or Password_length > 12:
    if Password_length < 6:
        Outputted_length = "too short"
        Outputted_criteria = "no shorter than 6 characters"
    else:
        Outputted_length = "too long"
        Outputted_criteria = "no longer than 12 characters"
    print("Your password is", Outputted_length, ". It has to be", Outputted_criteria, ".")
    Password = input("Please enter a password:")
    Password_length = len(Password)

    while Extended_presence:
        print("Your password contains characters from the extended ASCII list. Please don't use these.")
        Password = input("Please enter a password:")
        Extended_presence = hasExtended(Password)

while Extended_presence:
    print("Your password contains characters from the extended ASCII list. Please don't use these.")
    Password = input("Please enter a password:")
    Extended_presence = hasExtended(Password)

    while Password_length < 6 or Password_length > 12:
        if Password_length < 6:
            Outputted_length = "too short"
            Outputted_criteria = "no shorter than 6 characters"
        else:
            Outputted_length = "too long"
            Outputted_criteria = "no longer than 12 characters"
        print("Your password is", Outputted_length, ". It has to be", Outputted_criteria, ".")
        Password = input("Please enter a password:")
        Password_length = len(Password)
else:
    #defines function for checking for presence of numbers
    def hasNumbers(s):
        return any(i.isdigit() for i in s)
    #defines function for checking for presence of letters
    def hasLetters(s):
        return any(i.isalpha() for i in s)
    #defines function for checking for presence of special characters
    def hasSpecial(s):
        return any(ord(i) < 48 for i in s)
    #checks if password contains letters
    Letter_presence = hasLetters(Password)
    if not Letter_presence:
        Score = Score - 1
        Improvements.append("letters")
    else:
        Score = Score + 1
    #checks if password is all upper case
    Is_upper = Password.isupper()
    if not Is_upper:
        Score = Score + 1
    else:
        Score = Score - 1
        Improvements.append("upper and lower case letters")
    #checks if passwords is all lower case
    Is_lower = Password.islower()
    if not Is_lower:
        Score = Score + 1
    else:
        Score = Score - 1
        Improvements.append("upper and lower case letters")
    #checks if password contains a number
    Number_presence = hasNumbers(Password)
    if not Number_presence:
        Score = Score + 0
        Improvements.append("numbers")
    else:
        Score = Score + 1
    #checks if password is just numbers
    Only_numbers = Password.isdigit()
    if not Only_numbers:
        Score = Score + 0
    else:
        Score = Score - 1
        Improvements.append("other characters")
    #checks if password contains special characters
    Special_presence = hasSpecial(Password)
    if not Special_presence:
        Score = Score + 0
        Improvements.append("special characters, such as '$'")
    else:
        Score = Score + 1
    #outputs weak, medium or strong password to user and suggest improvements
    if Score <= 2:
        print("The program is processing your password...")
        time.sleep(2)
        print("Your password isn't acceptable! Please try again.")
        print("Next time, remember to include", Improvements)
    if Score == 3:
        print("The program is processing your password...")
        time.sleep(2)
        print("Your password is weak, you should try again.")
        print("Next time, remember to include", Improvements)
    elif Score == 4:
        print("The program is processing your password...")
        time.sleep(2)
        print("Your password is medium, it should be OK.")
        print("Next time, remember to include", Improvements)
    elif Score == 5:
        print("The program is processing your password...")
        time.sleep(2)
        print("Your password is strong, it is absolutely fine.")

Tags: toforiftimeispasswordlengthelse
3条回答

尝试此代码,而不是在第一次检查扩展\u存在的密码时使用if,您不需要while,因为它已经在前一个while循环的范围内:

import re
print([chr(i) for i in range(127,200)])

import re
import time
#score variable, responsible for later defining if passwords is weak, medium or strong
Score = 0
#list for improvements to password printed out with score at the end
Improvements = []
Password = ""
#defines function for checking for presence of extended characters (i.e. ascii 128-255)
def hasExtended(s):
    return any(ord(i) > 127 for i in s)
#inputs password
Password = input("Please enter a password:")
Password_length = len(Password)
Extended_presence = hasExtended(Password)
#checks password length (6-12), asks user to re-input until password is within boundaries
#checks if password contains extended characters
#double while loops to allow for two conditions simultaneously
while Password_length < 6 or Password_length > 12:
    if Password_length < 6:
        Outputted_length = "too short"
        Outputted_criteria = "no shorter than 6 characters"
    else:
        Outputted_length = "too long"
        Outputted_criteria = "no longer than 12 characters"
    print("Your password is", Outputted_length, ". It has to be", Outputted_criteria, ".")
    Password = input("Please enter a password:")
    Password_length = len(Password)

    if Extended_presence:
        print("Your password contains characters from the extended ASCII list. Please don't use these.")
        Password = input("Please enter a password:")
        Extended_presence = hasExtended(Password)

while Extended_presence:
    print("Your password contains characters from the extended ASCII list. Please don't use these.")
    Password = input("Please enter a password:")
    Extended_presence = hasExtended(Password)

输出:

Please enter a password:hel¢
Your password is too short . It has to be no shorter than 6 characters .
Please enter a password:hello¢
Your password contains characters from the extended ASCII list. Please don't use these.
Please enter a password:hello¢
Your password contains characters from the extended ASCII list. Please don't use these.
Please enter a password:hellllo#

我建议您编写验证器,一个如下所示的函数:

def validate_password(password):
    ... # check score, symbols and other, print whatever you want
    return score

然后用这样的方式称呼它:

pass = input('Enter your password')
score = validate_password(pass)
while score <= 3:
    pass = input('Enter another password')
    score = validate_password(pass)

您可以实现如下场景: check\u pass函数将检查函数的长度

def hasExtended(s):
    return any(ord(i) > 127 for i in s)

check_pass = lambda x: x>=6 and x<=12  # check for range of input value

password=input("Password: ")

if check_pass(len(password)) and not hasExtended(password):
    print("Valid password")
else:
    print("Do not use extended ascii characters, use password with minimum length of 6 and maximum length of 12")

为了更精确地描述错误,可以按以下方式使用嵌套:

if check_pass(len(password)) and not hasExtended(password):
    print("Valid password")
elif check_pass(len(password)) and hasExtended(password):
    print("do not use extended ascii character")
elif not check_pass(len(password)) and hasExtended(password):
    print("password length should be between 6 to 12")

同样,如果要分别检查最小长度和最大长度的无效长度。你知道吗

如果您希望单独检查所有条件,然后显示成功或失败,您可以执行以下操作:

def verify_password(password):
    if len(password) < 6:
        print("require minimum 6 characters")
    if len(password) > 12:
        print("can not use more then 12 characters")
    if hasExtended(password):
        print("use only valid ascii characters")
    else:
        print("valid password")
        return True

while not verify_password(password): # loop will never stops unless valid password or used break statement within
    password = input("Password: ")

如果条件满足消息将被打印,函数将检查三个不同的条件,否则它将继续执行,最后如果它是有效密码而不是打印任何内容,它将打印有效密码并返回True而不是None。你知道吗

相关问题 更多 >