在while循环中定义变量

3
我正在学习Python课程,但在使用while循环时遇到了问题。我的代码旨在检查密码是否具有最小长度=6和最大长度=14,并且还将检查密码是否仅包含数字或字母。如果它既包含数字又包含字母,则应打印“强密码”,如果它只包含数字或字母,则应打印“弱密码”。
MIN_PASSWORD_LENGTH = 6

MAX_PASSWORD_LENGTH = 14


while password_length >= MIN_PASSWORD_LENGTH or password_length <= MAX_PASSWORD_LENGTH:

password_length = len(password)

password = input("Enter your password: ")


if password.isalpha():

    print("Your password is weak!")

elif password.isnumeric():

    print("Your password is weak!")

else:

    print("Your password is strong!")


print("Number of characters used in password: ", password_length,"the min length expected is: ",MIN_PASSWORD_LENGTH,
"the max length is: ", MAX_PASSWORD_LENGTH)

当我运行我的代码时,出现错误信息:'name password_length is not defined'。我不确定该怎么办?我的代码是否正确?我是否应该将password_length放在while循环外面?

1
代码块可以使用缩进。(*提示:你在定义password_length之前就使用了它。) - TrebledJ
1
还建议您阅读要求用户输入直到他们给出有效响应 - martineau
6个回答

1
在 while 循环之前立即初始化,例如 "password_length = MAX_PASSWORD_LENGTH",否则 while 循环无法开始。while 循环内的第一行是 "password_length = len(password)",它将正确设置 password_length 的值,但是 while 循环需要一些初始值以便到达该点。

1

while循环中的条件不正确。您可以尝试:

MIN_PASSWORD_LENGTH = 6

MAX_PASSWORD_LENGTH = 14

password = input("Enter your password: ")
password_length = len(password)

while password_length < MIN_PASSWORD_LENGTH or password_length > MAX_PASSWORD_LENGTH:
    print("Password length", password_length, "incorrect. Required between", MIN_PASSWORD_LENGTH, "and", MAX_PASSWORD_LENGTH)
    password = input("Enter your password again: ")
    password_length = len(password)

if password.isalpha() or password.isnumeric():
    print("Your password is weak!")
else:
    print("Your password is strong!")

print("Number of characters used in password:", password_length,". The min length expected is:",MIN_PASSWORD_LENGTH, ". The max length is: ", MAX_PASSWORD_LENGTH)

1
你的想法基本正确,只是需要在循环之外为password_length分配一个值。请思考一下:当代码运行时,解释器会进入while循环并尝试进行涉及password_length的比较。但是此时password_length还不存在,因为它第一次获得值是在循环内部。因此,在进入循环之前,您应该将其初始化为一个合理的值,例如0。
两个补充说明:
1. 您正在计算上一个密码的密码长度,因此如果您输入的密码太短/太长,然后输入一个可接受的密码,则打印的长度将是不可接受的密码的长度。 2. 通常,最好使用f-strings或str.format调用而不是字符串连接,因此对于您的print,这可能更好些:
print(f'Number of characters used in password: {password_length}; '
       'the min length expected is {MIN_PASSWORD_LENGTH} and the '
       'max length expected is {MAX_PASSWORD_LENGTH}.')

1

您在定义 passwordpassword_length 变量之前使用了它们。

此外,您可以使用函数并重写它以使其更加结构化:

MIN_PASSWORD_LENGTH = 6
MAX_PASSWORD_LENGTH = 14

def checkPass():
    password = input("Enter your password: ")
    password_length = len(password)

    if password.isalpha():
        print("Your password is weak!")
    elif password.isnumeric():
        print("Your password is weak!")
    else:
        print("Your password is strong!")

    return password_length >= MIN_PASSWORD_LENGTH or password_length <= MAX_PASSWORD_LENGTH

while checkPass():
    continue

print("Number of characters used in password: ", password_length,"the min length expected is: ",MIN_PASSWORD_LENGTH, "the max length is: ", MAX_PASSWORD_LENGTH)

在线演示


1
问题在于您在代码了解密码是什么之前就调用了密码
MIN_PASSWORD_LENGTH = 6

MAX_PASSWORD_LENGTH = 14

# password must be defined before you use it. 
password = input("Enter your password: ")

while password_length >= MIN_PASSWORD_LENGTH or password_length <= MAX_PASSWORD_LENGTH:

password_length = len(password)  

if password.isalpha(): 
    print("Your password is weak!")

elif password.isnumeric(): 
    print("Your password is weak!")

else: 
    print("Your password is strong!")   

print("Number of characters used in password: ", password_length,"the min length expected is: ",MIN_PASSWORD_LENGTH,
"the max length is: ", MAX_PASSWORD_LENGTH)

1
这是我理解后基于它的一个版本。
MIN_PASSWORD_LENGTH = 6
MAX_PASSWORD_LENGTH = 14
password = ""
atLeast1Apha = False
atLeast1Numeric = False

while True:
    password = input("Enter your password: ")   
    if len(password) > MAX_PASSWORD_LENGTH or len(password) < MIN_PASSWORD_LENGTH:
        print("Password could be between %s and %s long" %(MIN_PASSWORD_LENGTH, MAX_PASSWORD_LENGTH))
        continue
    for char in password:
        if char.isalpha():
            atLeast1Apha = True
        if char.isdigit():    
            atLeast1Numeric = True
    break        

if atLeast1Apha and atLeast1Numeric:
    print("Your password is strong!")
else:
    print("Your password is weak!")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接