Back to Freecodecamp

Step 53

curriculum/challenges/english/blocks/learn-regular-expressions-by-building-a-password-generator/6564f583ca6fbf9556098dd6.md

latest1.1 KB
Original Source

--description--

Below the constraints list, add a comment saying Check constraints.

--hints--

You should add the provided comment.

js
({ test: () => assert.match(code, /#\s*Check\sconstraints/) })

--seed--

--seed-contents--

py
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)
--fcc-editable-region--        
        constraints = [
            (nums, r'\d'),
            (lowercase, r'[a-z]'),
            (uppercase, r'[A-Z]'),            
            (special_chars, fr'[{symbols}]')            
        ]
        
--fcc-editable-region--
    return password

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