PASSWORD VALIDATION | Sololearn: Learn to code for FREE!

+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 ('!', '@', '#', '$', '%', '&', '*'), 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: [email protected]$World19 Sample Output: Strong ''' password = input() import re pattern = r"[0-9]{2,}[[email protected]#$%&*]{2,2}" if len(password)>=7 and re.search(pattern,password): print ("Strong") else: print ("Weak") What's wrong in this code???

1/4/2021 3:26:02 AM

CHANDAN ROY

11 Answers

New Answer

+1

《 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".*[[email protected]#$%&*]{1}.*[[email protected]#$%&*]{1}.*" if len(password)>=7 and re.search(pattern1,password) and re.search(pattern2,password): print ("Strong") else: print ("Weak") Thanks a lot!!!

+3

password = input() import re pattern1 = r"[0-9]{2,}" pattern2 = r"[[email protected]#$%&*]{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.

+2

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"[[email protected]#$%&*]" 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.

+2

CHANDAN ROY Hello2wo#[email protected]

+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.

0

I Am AJ ! 《 Nicko12 》

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."

0

is this not working? I tried this and the problem is solved. https://code.sololearn.com/ccwGiV7xykIi/?ref=app

0

import re s = input() if len(s) >= 7: pattern = r"[0-9].*[0-9]" if re.search(pattern, s): pattern = r"[[email protected]#$%&*].*[[email protected]#$%&*]" if re.search(pattern, s): print('Strong') else: print('Weak') else: print('Weak') else: print('Weak')

0

Works like a charm 😊 i = list(input()) sc = list("[email protected]#$%&*") 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")

-2

(305) 600-7646