Can anyone help in finding error in code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can anyone help in finding error in code?

https://www.sololearn.com/coach/76?ref=app import re pattern = re. compile(r"(?=^.{7,}$)(?=.*[A-Za-z]{2})(?=.*[0-9]{2})(?=.*[!@#$%&*]{2})") string =input() if pattern.match(string): print ("Strong") else: print("Weak") https://code.sololearn.com/c2bxisRGHRB0/?ref=app I can't find the error in the code for password validation, well it gives correct output but didn't pass all the test.

1st Nov 2020, 7:28 AM
Aanisa Ali
Aanisa Ali - avatar
4 Answers
+ 4
A little late but I found the real problem, it is that the program in that way looks for the 2 special characters and numbers but >consecutively<, that is what gives the failure in the last case. The correct thing to do is to use parentheses to delimit the {2,} import re pattern = re. compile(r"(?=^.{7,}$)(?=(.*[0-9]){2})(?=(.*[!@#$%&*]){2})") string =input() if pattern.match(string): print ("Strong") else: print("Weak")
2nd Nov 2020, 8:38 AM
Mariano Fregosi
Mariano Fregosi - avatar
+ 4
Remove all three regex {2}
1st Nov 2020, 7:56 AM
Mariano Fregosi
Mariano Fregosi - avatar
+ 1
Thanku 😇 Can you explain it? Mariano Fregosi
1st Nov 2020, 7:59 AM
Aanisa Ali
Aanisa Ali - avatar
+ 1
To be honest I think there is some problem with the cases and the regex, because in java it is exactly the same. If you test the code in the playground as you had, for example with "qwert&5" it will give you that is "weak" and it is fine, now without the regex it will give a "strong" false positive. Maybe there is another reason, but it is beyond my knowledge 😅
1st Nov 2020, 8:09 AM
Mariano Fregosi
Mariano Fregosi - avatar