Reputation: 51
I going through a python course and am stuck at trying to use a while loop properly in my code
My code is supposed to check a password if it has min length =6 and max length =14, it will also check if the password only has numbers or letters. If it has a combination of both its supposed to print "strong password" if it only has numbers or letters it will print "weak password".
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)
When I run my code, it comes with error message: 'name password_length is not defined'. I am not sure what to do? Is my code even correct? Am I supposed to put the password_length outside the while loop?
Upvotes: 3
Views: 399
Reputation: 1121
Conditions in the while loop are wrong. You may try:
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)
Upvotes: 1
Reputation: 1683
Here is a version based on what I understood
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!")
Upvotes: 1
Reputation: 1151
You're using password
and password_length
variables before they are defined.
Also you can use function and rewrite it more structured:
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)
Upvotes: 1
Reputation: 828
The problem is your calling password before the code knows what it is.
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)
Upvotes: 1
Reputation: 4657
Immediately before the while loop, initialize with something like "password_length = MAX_PASSWORD_LENGTH" or else the while loop cannot start. The first line inside the while loop is "password_length = len(password)", which will set the value of password_length correctly, but the while loop needs something to start with so that you can reach that point.
Upvotes: 1
Reputation: 19895
You have, more or less, the right idea. It's just that you need to assign a value to password_length
outside your loop.
Think about this: when your code is run, the interpreter hits the while
loop and attempts to make a comparison involving password_length
. However, at that point password_length
doesn't exist yet, since the first time it gets a value is inside the loop. Therefore, you should initialise it to a sensible value, such as 0, before entering the loop.
Two supplementary points:
You're calculating the password length of the previous password, so if you enter a too-short/long password and then an acceptable one, the length printed will be of the unacceptable one.
In general, prefer f-strings or str.format
calls to string concatenation, so, for your print
, this might be better:
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}.')
Upvotes: 1