Learn Regular Expressions by Building a Password Generator - Step 57

Tell us what’s happening:

Its asking me to compare constraint and the length of the list returned by findall(), using the <= operator inside the for loop. I’ve tried flipping constraint and the length of the findall in the comparison, but to no avail.

for constraint, pattern in constraints:
len(re.findall(pattern, password))
constraint <= len(re.findall(pattern, password))

Any insight is appreciated!

Your code so far

import re
import secrets
import string


def generate_password(length, nums, special_chars, uppercase, lowercase):
    # Define the possible characters for the password
    letters = string.ascii_letters
    digits = string.digits
    symbols = string.punctuation

    # Combine all characters
    all_characters = letters + digits + symbols

    while True:
        password = ''
        # Generate password
        for _ in range(length):
            password += secrets.choice(all_characters)
       
        constraints = [
            (nums, r'\d'),
            (lowercase, r'[a-z]'),
            (uppercase, r'[A-Z]'),            
            (special_chars, fr'[{symbols}]')            
        ]

/* User Editable Region */

        # Check constraints
        for constraint, pattern in constraints:
            len(re.findall(pattern, password))
            constraint <= len(re.findall(pattern, password))

/* User Editable Region */

    return password

# new_password = generate_password(8)
# print(new_password)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36

Challenge Information:

Learn Regular Expressions by Building a Password Generator - Step 57

len(re.findall(pattern, password))

What does this line do? Doesn’t seem like you need it. I think you were to use it to build the comparison.

That’s exactly what it was, thank you.

1 Like

I got stuck in this step today, how should I write the code, I tried everything I could but to no avail, I don’t know what to do now I can’t proceed.

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Ask for Help button located on the challenge (it looks like a question mark). This button only appears if you have tried to submit an answer at least three times.

The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.