+ 2

Python password validation driving me crazy! help plz!

hi, I am trying to output password strength using python. But the code just doesn't want to work properly, so, are there any Python guru to solve the issue am having? Here is what i want to do; output a password must be more than 6 characters and less than 10 characters. The password has to be mix of letters and numbers. Here is my code: password = input("Enter password ") max_pass_length = 10 min_pass_length = 6 pass_len = len(password) while pass_len == max_pass_length or pass_len == min_pass_length: if pass_len > max_pass_length or pass_len < min_pass_length: password = input("enter password again ") else: if password.isnumeric(): msg = "error password only numbers" elif password.isalpha(): msg = "error password only letters" else: msg = "password strong!" print(msg) So, do you any of you know what i am doing wrong in here? cheers

21st May 2020, 4:02 AM
ALi_Abdul
ALi_Abdul - avatar
7 odpowiedzi
+ 7
Your 'while' statement is causing a problem. Unless the password is equal to the maximum or minimum length, the code stops there. I would take it out altogether (and move all your indents one step to the left).. Remember that in the Code Playground, all values have to be input before the code runs. If your code asks for a second input but doesn't get one at the beginning (I know, it seems crazy...) you will get an EOFError: EOF when reading a line. You might just want to treat a password that is too long or short the same way you treat one with numbers or letters only and simply tell the user what is wrong with it, e.g. too short. You could invite the user to try again, and they would simply run the code again. Cheers back. p.s. here's another way https://code.sololearn.com/cbV3CNmUih0C
21st May 2020, 5:02 AM
David Ashton
David Ashton - avatar
+ 7
ALi_Abdul In the Code Playground, you can only have the user run the code again. E.g. in my code I used if 8 > len(pw) or 32 < len(pw): print("Must be between 8 and 32 characters long.") quit()
22nd May 2020, 5:21 AM
David Ashton
David Ashton - avatar
+ 2
First thing is that you are checking password having len only 6 or 10 not in between them Second thing is that your while loop not terminating
21st May 2020, 4:57 AM
Mohd Aadil
Mohd Aadil - avatar
21st May 2020, 2:33 PM
Yurii Ostapenko
Yurii Ostapenko - avatar
+ 2
@David Ashton, Thank you for your explanation. I did put that while loop for a reason and the reason is to repeat the Password Input, if the password is not less than max_pass_length or greater than min_pass_length. Therefore, if i remove the while loop then how would I ask the user to re-type the password again. Thank you
21st May 2020, 3:20 PM
ALi_Abdul
ALi_Abdul - avatar
+ 2
@Mohd Aadil thank you for your explanation as well, I did changed the max and min length to (pass_len <= max_pass_length or pass_len => min_pass_length) and still have the same issue. And about the while loop I added it to ask the user to re-type the password in case the lengths don't match. Otherwise, how would I ask the user again to re-type the password. Thank you
21st May 2020, 3:25 PM
ALi_Abdul
ALi_Abdul - avatar
3rd Jun 2020, 4:15 PM
uday kiran
uday kiran - avatar