Learn Regular Expressions by Building a Password Generator - Step 61

Tell us what’s happening:

Instead of using a loop and a counter variable, you can achieve the same result with a different approach.

Replace your existing for loop and two if statements with a single if statement. For the if condition, use a list created with the list comprehension syntax.

The list stores the results of evaluating the expression constraint <= len(re.findall(pattern, password)) for each constraint-pattern tuple in the constraints list.

This is the request, but I don’t understand how I can delete the if e for statements with a comprehension syntax. I understand the comprehension syntax but I don’t understand how I can use this in this request.

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}]')            

        # Check constraints
        count = 0

# User Editable Region

       for constraint, pattern in constraints:
            if constraint <= len(re.findall(pattern, password)):
                count += 1
            if count == 4:

# 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/ Safari/537.36

Challenge Information:

Learn Regular Expressions by Building a Password Generator - Step 61

Hey, here you are going to implement the full logic in the following steps. For now you need to use a list comprehension as the condition of the new if, even if it does not make sense.

I have written the code though it’s not working.

Please open a new topic for your question and post your code for review. Won’t be able to help without that.

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.