Regular expression for password builder

Not able to convert for loop to list comprehensions in

Can someone please help

Please which of the steps is this? So we can help you better

what have you tried, what’s your code?

I’ve tried this

if len([(constraint, pattern) for constraint, pattern in constraints if constraint <= l

but it doesn’t seem right… But I don’t get it, any other python compiler runs it smoothly.
Here’s the whole code:

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
    if len([(constraint, pattern) for constraint, pattern in constraints if constraint <= len(re.findall(pattern, password))]) == 4:
        break

    #if len(new_constraint_list) == 4:
    #    break
return password

new_password = generate_password(8)

print(new_password)

Please open a new topic for your question, thanks!

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.

1 Like

Sorry I’m new to the platform. Back then I didn’t realise it would append my question to an already opened topic, I guess I thought the topics list was a sort of “category”. Thank you

1 Like