4

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.

GergelyPolonkai
  • 6,230
  • 6
  • 35
  • 69
John
  • 91
  • 3
  • 11
  • 5
    You might want to use a password strength checking module instead, here is one: https://pypi.python.org/pypi/password_strength – multivac Sep 30 '15 at 20:29
  • Sounds like Python RegEx is what you want. Try this: http://stackoverflow.com/a/2990682/5395008 – D. Smith Sep 30 '15 at 21:19

2 Answers2

6

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.

Prune
  • 76,765
  • 14
  • 60
  • 81
2

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"
user
  • 5,370
  • 8
  • 47
  • 75
Richard
  • 36
  • 1