Password checker containing many conditional statements Can this be shortened/improved? I'm trying to make a password checker in Python.

Could the ifs be put into a for loop?  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) &lt; 6:
    print("too short") 
elif len(pw) &gt; 12:
    print("too long")

if caps &gt;= 1:
    if lower &gt;= 1:
        if nums &gt;= 1:
            print(scr[2])
        elif nums &lt; 1:
            print("your password is " + scr[1])
    elif lower &lt; 1:
        print("your password strength is " + scr[0])
elif caps &lt; 1:
    print("your password strength is " + scr[1])
Enter password to test: premaintenance disdainful hayloft seer
too long
your password strength is medium

Enter password to test: NXJCWGGDVQZO
your password strength is weak

Enter password to test: Password1
strong


Your knowledge of password strength is: weak.

Explanation

Password strength is normally measured in "bits of entropy" — the idea being that if a password has been picked randomly from a pool of similar passwords of size N, then its entropy is log2N bits.

The first password I tried above was picked using a method suggested by Randall Munroe, like this:

&gt;&gt;&gt; words = list(open('/usr/share/dict/words'))
&gt;&gt;&gt; import random
&gt;&gt;&gt; random.SystemRandom().shuffle(words)
&gt;&gt;&gt; print(' '.join(w.strip() for w in words[:4]))
premaintenance disdainful hayloft seer


Its entropy can be calculated like this:

&gt;&gt;&gt; from math import log
&gt;&gt;&gt; l = len(words)
&gt;&gt;&gt; log(l * (l - 1) * (l - 2) * (l - 3), 2)
71.39088438576361


This is a strong password—a cracker that tried a billion such passwords a second would take on average about 50,000 years to find it.

The second password is also strong, but not as good as the first. I generated it like this:

$ &lt;/dev/random base64 | tr -cd A-Z | head -c 12
NXJCWGGDVQZO


Its entropy is 12 × log226 = 56.4 bits.

The third password is, of course, the weakest. password1 is about the 600th most common password (according to Mark Burnett, here) and the initial capital letter is a common substitution that password cracking programs know all about.