curriculum/challenges/english/blocks/learn-regular-expressions-by-building-a-password-generator/6565c13fdb798865c161d8f8.md
Now it's time to test your function. Uncomment the last two lines in your code and modify the function call passing 5 arguments. Use 8 for the length and 1 for the other four constraints.
You should call generate_password with the provided arguments.
({ test: () => assert.match(code, /^new_password\s*=\s*generate_password\s*\(\s*8\s*,\s*1\s*,\s*1\s*,\s*1\s*,\s*1\s*\)/m) })
You should print your new_password variable.
({ test: () => assert.match(code, /^print\s*\(\s*new_password\s*\)/m) })
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}]')
]
# 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(8)
# print(new_password)
--fcc-editable-region--