Learn Regular Expressions by Building a Password Generator - Step 57

Tell us what’s happening:

Describe your issue in detail here.

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


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

Challenge Information:

Learn Regular Expressions by Building a Password Generator - Step 57

2 Likes

You appear to have created this post without editing the template. Please edit your post to Tell us what’s happening in your own words.

Hello and welcome to the forum!

Please describe your problem when posting a question. The solution isn’t always as obvious as here:

You don’t have to repeat the length method, just compare constraint to the existing one.

wow. lets ignore that i couldnt solve that lol. :upside_down_face:

When you are learning Python as a second language, you might find yourself writing more code than necessary all the time, I speak from experience.

You first have to get used to Python abstracting a lot of the code away from you.

Hello! I have faced the same issues
here is my code: for constraint, pattern in constraints:
len(re.findall(pattern, password))
constraint <= len(re.findall(pattern, password))
i have tried so many other ways but none of them worked
for instance:
for constraint, pattern in constraints:
len(re.findall(pattern, password))
if constraint <= len(re.findall(pattern, password)):
pass
2) for constraint, pattern in constraints:
len(re.findall(pattern, password))
constraint <= len(re.findall())
none of these worked. Could you help me please ?

Hello

put constraint in front of the length method and compare both, all in the same line.

No extra code and no changes of the the original code.

1 Like

I solved it thank you!

I’m trying to understand the lesson topic:

Step 57

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

Check constraints

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

Please open a new topic

In the future, please create your own topic when you have specific questions about your own challenge code. Only respond to another thread when you want to provide help to the original poster of the other thread or have follow up questions concerning other replies given to the original poster.

The easiest way to create a topic for help with your own solution is to click the Ask for Help button located on each challenge. This will automatically import your code in a readable format and pull in the challenge url while still allowing you to ask any question about the challenge or your code.

Thank you.