Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python password checker: numbers and symbols

I'm new to python and I'm having a problem. I need to check a password on its upper, lower, numbers and symbols. It needs 1 of each class and the whole password needs to be longer then 6 characters. I have upper and lower case so far. raw_input inputs as a string so how can I then check for numbers and symbols in that string?

My code so far:

p = raw_input(("plz enter a password to check it's strength"))

if len (p) <= 6:
    if p.isupper() or p.islower() or int(p):
        print"This is a weak password  "
elif len(p) > 6 and len(p)  <26:
    if p.isupper() or p.islower() or isdigit():
        print "wea2k"
    else:
        print "good"

So what I need to know is how to check the input for numbers and symbols.

like image 533
John Avatar asked Feb 09 '23 22:02

John


2 Answers

Try taking the requirements one at a time.

has_upper = False
for char in p:
    if char.isupper():
        has_upper = True
        break

Repeat this for lower case and digits. For special characters, use the same type of loop, but change the if to something like this:

if not (char.isupper() or char.islower() or char.isdigit()):

At the end, if all four flags are true, then you have a strong password; otherwise, it's weak.

Can you finish the coding from there?


Just so you know, there are ways to code this that are far more "Pythonic" -- that use the language's style much better. For instance:

has_upper = reduce(lambda a, b: a or b.isupper(), [_ for _ in p], False)

... replaces the entire 5-line block that I gave you.

like image 171
Prune Avatar answered Feb 11 '23 10:02

Prune


Try this:

p = raw_input(("plz enter a password to check it's strength"))

upper_case = 0
lower_case = 0
number = 0
symbol = 0

for i in p:
    if i.isupper():
        upper_case += 1
    elif i.islower():
        lower_case += 1
    elif i.isdigit():
        number += 1
    else:
        symbol += 1

if len (p) <= 6:
    print"This is a weak password  "
elif upper_case > 0 and lower_case > 0 and number > 0 and symbol > 0:
    print "Good"
else:
    print "Too Weak"
like image 37
Richard Avatar answered Feb 11 '23 12:02

Richard