Learn Regular Expression by Building a Password Generator step 67

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'),
            (special_chars, fr'[{symbols}]'),
            (uppercase, r'[A-Z]'),
            (lowercase, r'[a-z]')
        ]

        # Check constraints        
        if all(
            constraint <= len(re.findall(pattern, password))
            for constraint, pattern in constraints
        ):
            break
    
    return password
    
new_password = generate_password(length=8, nums=1, special_chars=1, uppercase=1, lowercase=1)
print(new_password)

please help me !
i dont know what is the change order for this course in step 67

im already change the length and nums, but still not pass

Please format your code in triple backticks, like so:

```py
Your code here...
```

Thanks.

can you give a link to the step?


I’ve edited your post to improve the readability of the code. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Welcome to the forum @rahmad12360

You should have the (nums, '[0-9]') tuple in your constraints list.

Also, remove the rest of the tuples. Then instructions did not ask you to add them in this step.

I changed the category to Python to better reflect the nature of this post.

Happy coding

thank you, you helped me a lot

1 Like