Learn Regular Expressions by Building a Password Generator - Step 57

Is this not the right way?

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):
                pass

# User Editable Region

    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/605.1.15 (KHTML, like Gecko) Version/17.2.1 Safari/605.1.15

Challenge Information:

Learn Regular Expressions by Building a Password Generator - Step 57

The requirement says:

Inside your for loop, compare constraint and the length of the list returned by findall(). Use the <= operator for that.

You implemented as:

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

Unfortunately, the step instruction assumes much and it is not clear that compare constraint means constraint will become the first operand term and that length of is the second operand term if the operator <= is to be used.
In other words it must be, is constraint less or equal to the length of the list? Your implementation is read as, is the length of list less than or equal to constraint?

This didn’t work

constraint <= length of the list <= constraint

constraint <= length of the list
Reverse what you had before. length of the list is a place holder for what you did with len

so this
constraint <= len

1 Like

Initially you had this:

len(re.findall(pattern, password))

It is asking you to compare constraint as the first operand to that , using the <=.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.