0
Password Validator
password = input() import re #length 7 characters if len(password) <= 6: print ("Weak") #min 2 numbers elif re.search(r'\d{2,}',password) == None: print ("Weak") #2 special characters !@#$%&* elif re.search(r'[!@#$%&*]{2,}',password) == None: print ("Weak") else: print ("Strong") All test case pass except case 13. Please give some advice thank you.
12 Answers
+ 2
The metacharacters \d{2,} mean two or more consecutive numbers.
My way of solving this was to say:
...
elif len(re.findall(r"\d", password)) >= 2:
In this example, findall() finds each numeric character in password, and saves it into a list. If that list has length of at least 2, it's good. 👍
Same with the special characters, obviously.
+ 1
Your code is checking for the 2 numbers and 2 special characters to be next to each other, which is not required in the task.
+ 1
Vijay "Always try to use minimum lines of code" - why on earth do you think this? This is not good advice to anyone, beginner or expert. You should write code so it is structured, readable and understandable.
Also, your code doesn't work because it only checks for one number and one special character.
password: hello3! - output strong.
+ 1
Why u using regular expressions
Make the code more readable like mine
le = input()
specialchar = ['!', '@', '#', '#x27;, '%', '&', '*']
nums = [str(i) for i in range(10)]
def validator(phrase):
speccount =0
numscount =0
for letter in phrase:
if letter in specialchar:
speccount += 1
if letter in nums:
numscount +=1
if len(phrase) >= 7:
if speccount >=2 and numscount>=2:
print("Strong")
elif speccount < 2 and numscount < 2:
print("Weak")
else:
print("Weak")
else:
print("Weak")
validator(le)
0
What was the test case 13?
0
Russ how do I fix that ?
0
U have used too many lines for solving this problem and also {2,}
dictating the program to find 2 consecutive numbers and special characters in the input string.
That's why case 13 displaying error.
Just look my script :
(Always remember try to use minimum lines of code
for a particular problem.)
https://code.sololearn.com/cBBy35bFf49M/?ref=app
0
I have fixed that bug .
So, u can check out and if you find something is missing leave a note .
0
Vijay, you could even use just two lines of code if you wanted:
from re import match
print("Strong" if match(r"(?=.{7,})(?=(.*\d){2,})(?=(.*[!@#$%&*]){2,})", input()) else "Weak")
But, like Russ mentioned, the shortest anwser is not always the most easily understandable one.
However I do like the use of lookahead and based my solution on it as well.
https://code.sololearn.com/c8MG5M1hZ0ZF/?ref=app
0
Can anyone post the question please
0
I went ahead and solved this with string methods:
password = str(input())
password_length = len(password)
password_numbers = sum(c.isdigit() for c in password)
password_letters = sum(c.isalpha() for c in password)
password_spaces = sum(c.isspace() for c in password)
password_others = password_length - password_numbers - password_letters - password_spaces
a = bool(password_length >= 7)
b = bool(password_numbers >= 2)
c = bool(password_others >= 2)
if a and b and c:
print('Strong')
else:
print('Weak')
0
import re
inp_password = input()
digit=0
for char in inp_password:
if char.isdigit():
digit=digit+1
else:
pass
search_symbol = re.compile('[@$!#%&*]')
listOfmatches = regexPattern.findall(inp_password)
password_length = len(inp_password)
if len(listOfmatches) >= 2 and password_length > 7 and digit >=2:
print('Strong')
else:
print('Weak')