+ 3
Password Validation
Hi, I’m trying to complete the Password Validation exercise using Python. My Python code passes all tests except #13. Does anyone know what is wrong? My code is: Import re password = input("") pattern= r"[0-9]{2,}" pattern2= r"[%#*!@&$]{2,}" if len(password) >=7: if re.search(pattern, password): if re.search(pattern2, password): print("Strong") else: print("Weak") else: print("Weak") else: print("Weak")
7 Réponses
+ 2
This code with regex is okay and regex makes it easy to solve. I have coded a solution with regex in c# and this works. So I think there can be a problem in code coach.
+ 2
U can read learn something from this code 👇👇
number = 0
special = 0
password = input()
specialcharacter = "!@#$%&*"
allthenumbers = "0123456789"
for a in password:
if a in allthenumbers:
number = number+1
elif a in specialcharacter:
special = special+1
if len(password) >= 7 and number >= 2 and special >= 2:
print("Strong")
else:
print("Weak")
+ 1
it's pretty complicated when you use regex. try iterating through two lists and increasing a counter when the input string matches any of the values in the list:
symbols = ['!', '@', '#', '#x27;, '%', '&', '*']
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
sym_count = 0
num_count = 0
i remember you need both counters at least to the value of 2. so iterating through the input string and checking if there are any matches will solve this issue
after the iteration logic all you need is a simple if else condition in which you check if all the 3 conditions are true: length of input string greater or equal to 7, sym_count greater or equal to 2, num_count greater or equal to 2. if these are all true then you print('Strong'), if they aren't then it's 'Weak'
+ 1
Okay, I'll tell you my secret. Hope this helped you.
pattern= r"[0-9]+"
pattern2= r"[%#*!@&$]+"
0
Rithea Sreng I said similar solution with regex in C# works for me. Unfortunately, it is not understandable why the above mentioned fails in the 13 test.
0
Yeah I'have tried this jon but it gives error when the raw code is not in sequence the, pattern1 matches if only if 2 consecutive numbers are given otherwise it doesn't match. The same is applicable for pattern2.so try giving this as input "1df$3f#" it will give wrong output weak but it is strong it has 2 nums and 2 special char
0
password = list(input())
str = " ".join(password) #a string with space b/w the items
import re
symb = re.findall("[!@#\$%&\*]+", str)
num = re.findall(r"[0-9]+", str)
filt = filter((lambda x: x in symb,str), (password))
print ("Strong" if len(symb) >= 2 and len(num) >= 2 and len(password) >=7 else "Weak")