0

Password Validator

password = input() import re #length 7 characters if len(password) <= 6: print ("Weak") #min 2 numbers elif re.search(r'\d{2,}',password) == None: print ("Weak") #2 special characters !@#$%&* elif re.search(r'[!@#$%&*]{2,}',password) == None: print ("Weak") else: print ("Strong") All test case pass except case 13. Please give some advice thank you.

3rd Jun 2020, 12:22 AM
Toh De Kai
Toh De Kai - avatar
13 Answers
+ 2
The metacharacters \d{2,} mean two or more consecutive numbers. My way of solving this was to say: ... elif len(re.findall(r"\d", password)) >= 2: In this example, findall() finds each numeric character in password, and saves it into a list. If that list has length of at least 2, it's good. 👍 Same with the special characters, obviously.
5th Jun 2020, 6:56 AM
Russ
Russ - avatar
+ 1
Your code is checking for the 2 numbers and 2 special characters to be next to each other, which is not required in the task.
4th Jun 2020, 4:44 PM
Russ
Russ - avatar
+ 1
Vijay "Always try to use minimum lines of code" - why on earth do you think this? This is not good advice to anyone, beginner or expert. You should write code so it is structured, readable and understandable. Also, your code doesn't work because it only checks for one number and one special character. password: hello3! - output strong.
3rd Sep 2020, 5:58 AM
Russ
Russ - avatar
+ 1
Why u using regular expressions Make the code more readable like mine le = input() specialchar = ['!', '@', '#', '
#x27;, '%', '&', '*'] nums = [str(i) for i in range(10)] def validator(phrase): speccount =0 numscount =0 for letter in phrase: if letter in specialchar: speccount += 1 if letter in nums: numscount +=1 if len(phrase) >= 7: if speccount >=2 and numscount>=2: print("Strong") elif speccount < 2 and numscount < 2: print("Weak") else: print("Weak") else: print("Weak") validator(le)
25th Oct 2020, 6:35 AM
Eon
Eon - avatar
0
What was the test case 13?
3rd Jun 2020, 12:39 AM
Laopigo
Laopigo - avatar
0
Russ how do I fix that ?
5th Jun 2020, 12:17 AM
Toh De Kai
Toh De Kai - avatar
0
U have used too many lines for solving this problem and also {2,} dictating the program to find 2 consecutive numbers and special characters in the input string. That's why case 13 displaying error. Just look my script : (Always remember try to use minimum lines of code for a particular problem.) https://code.sololearn.com/cBBy35bFf49M/?ref=app
3rd Sep 2020, 5:06 AM
Vijay
Vijay - avatar
0
I have fixed that bug . So, u can check out and if you find something is missing leave a note .
3rd Sep 2020, 12:17 PM
Vijay
Vijay - avatar
0
Vijay, you could even use just two lines of code if you wanted: from re import match print("Strong" if match(r"(?=.{7,})(?=(.*\d){2,})(?=(.*[!@#$%&*]){2,})", input()) else "Weak") But, like Russ mentioned, the shortest anwser is not always the most easily understandable one. However I do like the use of lookahead and based my solution on it as well. https://code.sololearn.com/c8MG5M1hZ0ZF/?ref=app
9th Apr 2021, 5:47 PM
Khidr
0
Can anyone post the question please
31st May 2021, 4:00 PM
Nana King Obedson
Nana King Obedson - avatar
0
I went ahead and solved this with string methods: password = str(input()) password_length = len(password) password_numbers = sum(c.isdigit() for c in password) password_letters = sum(c.isalpha() for c in password) password_spaces = sum(c.isspace() for c in password) password_others = password_length - password_numbers - password_letters - password_spaces a = bool(password_length >= 7) b = bool(password_numbers >= 2) c = bool(password_others >= 2) if a and b and c: print('Strong') else: print('Weak')
28th Jul 2021, 10:07 AM
Snarkly
Snarkly - avatar
0
import re inp_password = input() digit=0 for char in inp_password: if char.isdigit(): digit=digit+1 else: pass search_symbol = re.compile('[@$!#%&*]') listOfmatches = regexPattern.findall(inp_password) password_length = len(inp_password) if len(listOfmatches) >= 2 and password_length > 7 and digit >=2: print('Strong') else: print('Weak')
29th Mar 2022, 10:13 AM
Anjana Suresh