0

So I'm trying to run a for loop on a dictionary containing regular expression objects in python but I'm getting the following error:

============ RESTART: C:/Users/JSurzyn/Scripts/pw_verify_neat.py ============
Enter a password >>>
Jordan93
Traceback (most recent call last):
  File "C:/Users/JSurzyn/Scripts/pw_verify_neat.py", line 18, in <module>
    for key, value in reg_dict:
TypeError: '_sre.SRE_Pattern' object is not iterable

Are regex objects not iterable? Any help would be great. I'm running python 3.6 on a Windows 10 64 bit machine.

For context. I'm trying to take the following code

import re

pw = str(input('Enter a password >>>\n' ))

length_regex = re.compile(r'.{8,}')
upper_regex = re.compile(r'[A-Z]')
lower_regex = re.compile(r'[a-z]')
num_regex = re.compile(r'[0-9]')

def t_pw(pw):
    try:
        mo = length_regex.search(pw)
        fo = mo.group()
        try:
            mo = upper_regex.search(pw)
            fo = mo.group()
            try:
                mo = lower_regex.search(pw)
                fo = mo.group()
                try:
                    mo = num_regex.search(pw)
                    fo = mo.group()
                    print('password approved')
                except:
                    print('password must have at least one numerical digit')
            except:
                print('password must have at least one lower case letter')
        except:
            print('password must have at least one upper case letter')

    except:
        print('password must be at least 8 characters.')


t_pw(pw)

And clean it up to look like:

import re

pw = str(input('Enter a password >>>\n' ))

length_regex = re.compile(r'.{8,}')
upper_regex = re.compile(r'[A-Z]')
lower_regex = re.compile(r'[a-z]')
num_regex = re.compile(r'[0-9]')

l_string = 'password must be at least 8 characters.'
u_string = 'password must have at least one upper case letter'
lo_string = 'password must have at least one lower case letter'
n_string = 'password must have at least one numerical digit'

reg_dict = {length_regex : l_string, upper_regex : u_string,
            lower_regex : lo_string, num_regex : n_string}

for key, value in reg_dict:
    try:
        mo = key.search(pw)
        fo = mo.group()
    except:
        print(value)
        exit()

print('Password approved')

0 Answers0