Back to Freecodecamp

Step 54

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

latest1.6 KB
Original Source

--description--

After your new comment, write a for loop to iterate over the constraints list. Use constraint and pattern as the loop variables.

--hints--

You should write a for loop to iterate over the constraints list.

js
({ test: () => assert(runPython(`_Node(_code).find_function("generate_password").find_whiles()[0].find_bodies()[0].find_for_loops()[1].find_for_iter().is_equivalent("constraints")`)) })

Your for loop should use constraint and pattern as the loop variables to iterate over the constraints list.

js
({ test: () => assert(runPython(`_Node(_code).find_function("generate_password").find_whiles()[0].find_bodies()[0].find_for_loops()[1].find_for_vars().is_equivalent("constraint, pattern")`)) })

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

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