+ 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???14 Antworten
+ 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!!!
+ 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.
+ 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.
+ 2
CHANDAN ROY
Hello2wo#rl3d@
+ 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.
+ 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')
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"[!@#$%&*].*[!@#$%&*]"
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("!@#$%&*")
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")
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")
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
- 2
(305) 600-7646