Learn Regular Expressions by Building a Password Generator - Step 61

Tell us what’s happening:

Maybe my brain isn’t working at full capacity right now, or maybe I’m missing something but I just don’t seem to understand what the exercise is asking of me right now. I removed the loops, and I added the (what I think may be correct) if statement but I can’t seem to implement the next part because I simply don’t understand what it’s asking of me. Any tips would be appreciated. Thank you in advance.

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

        if constraint <= len(re.findall(pattern, password)):
            

# User Editable Region

            break

    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 OPR/106.0.0.0

Challenge Information:

Learn Regular Expressions by Building a Password Generator - Step 61

Hey, that’s quite tricky. You are going to build the full logic in the next steps, so this might not make sense right now. The condition of the if should be a list comprehension instead of this:

The list comprehension you need to create should evaluate the above for each constraint, pattern in the constraints list.
Hope this helps.

Thank you for replying to me, and sorry to bother again. I tried a few more methods after reading up on the functions some more online, and I still can’t seem to get it to work. I’m just confused on how I’m supposed to write it. I don’t remember doing this earlier in the course so I’m just a tiny bit confused. Can you please explain it to me? I just want to understand.

A list comprehension is the opposite syntax of a for loop.

for item in list:
    print(item)

but the list comprehension is in the opposite order:

[print(item) for item in list]

You need to transform this for loop into a list comprehension in the same way.

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

You should end up with:

if <list comprehension>:

So I tried something along the lines of :

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

And several combinations before and after as well, am I missing something?
…Or am I just stupid?
I know I must definetly missing something, or is it all wrong?

Looks great :+1: but a list comprehension (and a list) uses square brackets [ ]

https://www.w3schools.com/python/python_lists_comprehension.asp

https://www.geeksforgeeks.org/python-list-comprehension/

Thank you! I really missed that small detail. I appreciate everyone’s help. Happy coding.