Back to Freecodecamp

Step 60

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

latest1.9 KB
Original Source

--description--

Finally, after the for loop, create an if statement to check if count is equal to 4 and break out of the while loop by using the break statement.

--hints--

You should create an if statement that checks if count is equal to 4 after the for loop.

js
({ test: () => assert.match(code, /^(\s*)for.+:\s*^\1(\s{4})if\s+constraint\s*<=\s*len\s*\(\s*re\.findall\s*\(\s*pattern\s*,\s*password\s*\)\s*\)\s*:\s*^\1\2\2count\s*\+=\s*1\s*^\1if\s+count\s*==\s*4\s*:/m) })

You should use break inside your new if to break out of the while loop.

js
({ test: () => {
    const commentless_code = __helpers.python.removeComments(code);
    const {block_body} = __helpers.python.getBlock(commentless_code, /if\s+count\s*==\s*4\s*/);
    assert(block_body.match(/^\s+break\s*$/m));  
  }
})

--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
        count = 0
        for constraint, pattern in constraints:
            if constraint <= len(re.findall(pattern, password)):
                count += 1
--fcc-editable-region--
    return password

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