Back to Freecodecamp

Step 71

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

latest1.4 KB
Original Source

--description--

Modify your print() call to take the string 'Generated password:' as the first argument, before new_password.

--hints--

You should pass the string 'Generated password:' and new_password to your print() call.

js
({ test: () => assert.match(code, /^print\s*\(\s*("|')Generated\spassword:\1\s*,\s*new_password\s*\)/m) })

--seed--

--seed-contents--

py
import re
import secrets
import string


def generate_password(length=16, nums=1, special_chars=1, uppercase=1, lowercase=1):

    # 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
    
--fcc-editable-region--
new_password = generate_password()
print(new_password)
--fcc-editable-region--