Learn Regular Expressions by Building a Password Generator - Step 61

Tell us what’s happening:

I simply don’t know what else to do. Have tried many different options and still get nothing.

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)) for constraint, pattern in constraints]:
            
            
                

# User Editable Region

            break

    return password

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36

Challenge Information:

Learn Regular Expressions by Building a Password Generator - Step 61

I am reffering myslef to this line of code →
if [constraint <= len(re.findall(pattern, password)) for constraint, pattern in constraints]:

This step has been updated. I suggest you to clear the browser cache. The updated instructions are:

Instead of using a loop and a counter variable, you can achieve the same result with a different approach, which you are going to implement in the next few steps.
all() is a built-in Python function that returns True if all the elements inside a given iterable evaluate to True. Otherwise, it returns False.
Replace your existing for loop and two if statements with a single if statement. For the if condition, use a call to the all() function and pass an empty list as the argument to the function call.

this is the step that I have:

Step 61

Instead of using a loop and a counter variable, you can achieve the same result with a different approach, which you are going to implement in the next few steps.

all() is a built-in Python function that returns True if all the elements inside a given iterable evaluate to True. Otherwise, it returns False.

Replace your existing for loop and two if statements with a single if statement. For the if condition, use a call to the all() function and pass an empty list as the argument to the function call.

is the same one as yours

And my code still not works

If it is the same, tell me: how did you come up with this? Please.

well because I am demanded to use a list comprehension composed of the code, arguments, and iterable.

Did you actually read this?

Yes, but I have tried to apply it like this and it doesnt work:

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

you are not passing an empty list to all()

Yeah I know, the thing is I don’t get how to do it haha, could explain any further?

how do you write an empty list?

like this [], right ?

* , but how to implement it in the line of code is my difficulty

put it as argument of all()

you need to format it with backticks to avoid mardown taking ownership of the empty list and making it a checkbox

sorry but i simply don’t get it :frowning:

you have a function, all(), a function can get arguments, right? you give argumetns to a function by putting the value between the parenthesis. So put the empty list inside the round parenthesis of the all function so that in the if condition you are calling all with an empty list as argument

already got it, thanks hahaha. I was overcomplicating myself