在while循环中定义变量

时间:2019-03-25 01:10:00

标签: python

我正在上一门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)

当我运行代码时,它带有错误消息:“未定义名称password_length”。我不确定该怎么办?我的代码是否正确?我是否应该将password_length放在while循环之外?

6 个答案:

答案 0 :(得分:0)

您或多或少都有正确的想法。只是您需要在循环外为password_length分配一个值。

请考虑一下:运行代码时,解释器将进入while循环并尝试进行涉及password_length的比较。但是,此时password_length尚不存在,因为它第一次获取值是在循环内 。因此,在进入循环之前,应将其初始化为一个合理的值,例如0。

两个补充要点:

  1. 您正在计算上一个密码的密码长度,因此,如果输入的密码太短/太长,然后输入一个可以接受的密码,则打印的长度将是不可接受的一个。

  2. 通常,更喜欢f字符串或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 :(得分:0)

立即在while循环之前,使用“ password_length = MAX_PASSWORD_LENGTH”之类的东西进行初始化,否则while循环无法启动。 while循环内的第一行是“ password_length = len(password)”,它将正确设置password_length的值,但是while循环需要一些开头,以便您可以到达该点。

答案 2 :(得分:0)

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)

答案 3 :(得分:0)

在定义变量之前,您正在使用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)

Online demo

答案 4 :(得分:0)

问题在于您在代码知道是什么之前调用了密码

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)

答案 5 :(得分:0)

这是根据我所了解的版本

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!")