PASSWORD VALIDATION | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

PASSWORD VALIDATION

''' Task: Write a program that takes in a string as input and evaluates it as a valid password. The password is valid if it has at a minimum 2 numbers, 2 of the following special characters ('!', '@', '#', '

#x27;, '%', '&', '*'), and a length of at least 7 characters. If the password passes the check, output 'Strong', else output 'Weak'. Input Format: A string representing the password to evaluate. Output Format: A string that says 'Strong' if the input meets the requirements, or 'Weak', if not. Sample Input: Hello@$World19 Sample Output: Strong ''' password = input() import re pattern = r"[0-9]{2,}[!@#$%&*]{2,2}" if len(password)>=7 and re.search(pattern,password): print ("Strong") else: print ("Weak") What's wrong in this code???

4th Jan 2021, 3:26 AM
CHANDAN ROY
CHANDAN ROY - avatar
14 Answers
+ 4
《 Nicko12 》 Thanks for your input brother. You explained 2 vital concepts needed to solve this challenge. (I) Order (ii) seperated test cases With the help of these inputs, I have finally solved this challenge. My code is - password = input() import re pattern1 = r".*[0-9]+.*[0-9]+.*" pattern2 = r".*[!@#$%&*]{1}.*[!@#$%&*]{1}.*" if len(password)>=7 and re.search(pattern1,password) and re.search(pattern2,password): print ("Strong") else: print ("Weak") Thanks a lot!!!
4th Jan 2021, 9:21 AM
CHANDAN ROY
CHANDAN ROY - avatar
+ 5
password = input() import re pattern1 = r"[0-9]{2,}" pattern2 = r"[!@#$%&*]{2,}" if len(password)>=7 and re.search(pattern1,password) and re.search(pattern2,password): print ("Strong") else: print ("Weak") # Patterns are seperated so that it will just search for numbers and symbols regardless of the order. The reason why your pattern does not work in other test cases is because the condition will only become true if the number comes first before the symbol.
4th Jan 2021, 5:03 AM
noteve
noteve - avatar
+ 3
CHANDAN ROY Oh ok, maybe the reason is the last test cases' number or symbol have letters in between i.e. , seperated. Try this: password = input() import re pattern1 = r"[0-9]" pattern2 = r"[!@#$%&*]" if len(password)>=7 and len(re.findall(pattern1,password)) >= 2 and len(re.findall(pattern2,password)) >= 2: print ("Strong") else: print ("Weak") # re.findall creates a list containing elements that satisfy the pattern. We used "len" method to know how many numbers or symbols are there.
4th Jan 2021, 7:02 AM
noteve
noteve - avatar
+ 2
CHANDAN ROY Hello2wo#rl3d@
4th Jan 2021, 7:37 AM
noteve
noteve - avatar
+ 1
《 Nicko12 》 That explanation was really helpful and it did solve one of the two tests that I had failed. Most importantly, I learnt something I had missed in the lesson though it was explained there. So, thanks a lot!! It still fails one last hidden condition.
4th Jan 2021, 5:50 AM
CHANDAN ROY
CHANDAN ROY - avatar
+ 1
rd = ['!','@','#','
#x27;,'%','&','*'] xpassword = input() res = 0 nums = 0 for x in xpassword: if x in '!@#$%&*': res += 1 if x in '1234567890': nums += 1 if res >= 2 and len(xpassword) >= 7 and nums >= 2: print('Strong') else: print('Weak')
2nd Jun 2023, 7:11 AM
IsantosDowd
IsantosDowd - avatar
4th Jan 2021, 4:40 AM
CHANDAN ROY
CHANDAN ROY - avatar
0
《 Nicko12 》 Give an example of what you just said in opening line of your previous comment "maybe the reason is the last test cases number of symbol have letters in between I.e.,seperated."
4th Jan 2021, 7:08 AM
CHANDAN ROY
CHANDAN ROY - avatar
0
is this not working? I tried this and the problem is solved. https://code.sololearn.com/ccwGiV7xykIi/?ref=app
4th Jan 2021, 9:09 AM
noteve
noteve - avatar
0
import re s = input() if len(s) >= 7: pattern = r"[0-9].*[0-9]" if re.search(pattern, s): pattern = r"[!@#$%&*].*[!@#$%&*]" if re.search(pattern, s): print('Strong') else: print('Weak') else: print('Weak') else: print('Weak')
4th Jan 2021, 3:03 PM
QerdeX
0
Works like a charm 😊 i = list(input()) sc = list("!@#$%&*") n = list("0123456789") cSc = 0 cN = 0 for x in i: if x in sc: cSc += 1 if x in n: cN += 1 if cSc >= 2 and cN >= 2 and len(i) >= 7: print("Strong") else: print("Weak")
30th Jun 2022, 7:33 AM
Martin Valdés Mallaug
0
import re password = input() symbols_list = re.findall('[-!@#$%&*]', password ) number_list = re.findall("[0-9]", password ) if len(password) >= 7 and len(symbols_list ) >= 2 and len(number_list )>=2: print("Strong") else: print("Weak")
6th Sep 2022, 8:27 AM
yonis Alvarez
yonis Alvarez - avatar
0
import re psswd=str(input()) regex='\d\d{2}' regex='[!,@,#,&,$,*,%]' result=re.search(regex,psswd) if len(psswd)>7 and result: print("Strong") elif len(psswd)>=7 and not result: print("Weak") else: print("Weak") Why wouldn't this code run
3rd Mar 2023, 10:21 AM
Nkeh Bebey
Nkeh Bebey - avatar
- 2
(305) 600-7646
4th Jan 2021, 2:46 PM
Joshua Parker
Joshua Parker - avatar