in this piece of code the program is asking to enter the password,password must have at least 8 char and with numbers and letters,if any of these condition did not meet so the program give warning and ask again to enter the password! so when the password is met the condition ,the loop does not stop and ask again!! I'd like to know how can I stop it when the password is correct?
def passValidation(password):
if len(password) < 8 :
print('ur password must be at least 8 char :')
elif password.isnumeric():
print('ur password must have one letter')
elif password.isalpha():
print('ur password must have one number')
else :
print('ur password is true')
while True :
password = input('enter ur password:')
passValidation(password)
It's fine for your passValidation
to print out useful statements indicating how the password is invalid, but ultimately it should return
True
or False
to indicate password validity.
Second, you don't necessarily have to enter into a loop if the first input password is fine in the first place. Instead of immediately jumping into a while loop, prompt for a password then enter into the loop if it's invalid. This makes it clearer what your while
condition should be rather than using while True
with a break
statement.
def passValidation(password):
if len(password) < 8 :
print('ur password must be at least 8 char :')
return False
elif password.isnumeric():
print('ur password must have one letter')
return False
elif password.isalpha():
print('ur password must have one number')
return False
else:
print('ur password is true')
return True
password = input('enter ur password:')
while not passValidation(password):
password = input('enter ur password:')