can this code be shortened or improved?

Posted by user2816683 on Stack Overflow See other posts from Stack Overflow or by user2816683
Published on 2013-10-25T21:28:29Z Indexed on 2013/10/25 21:54 UTC
Read the original article Hit count: 114

Filed under:
|

Can this be shortened/improved? I'm trying to make a password checker in python.

Could the if's be put into a for loop? And if so, how?

pw = input("Enter password to test: ")

caps = sum(1 for c in pw if c.isupper())
lower = sum(1 for c in pw if c.islower())
nums = sum(1 for c in pw if c.isnumeric())

scr = ['weak', 'medium', 'strong']
r = [caps, lower, nums]


if len(pw) < 6:
    print("too short") 
elif len(pw) > 12:
    print("too long")

if caps >= 1:
    if lower >= 1:
        if nums >= 1:
            print(scr[2])
        elif nums < 1:
            print("your password is " + scr[1])
    elif lower < 1:
        print("your password strength is " + scr[0])
elif caps < 1:
    print("your password strength is " + scr[1])

Thanks for any suggestions :D

© Stack Overflow or respective owner

Related posts about python

Related posts about passwords