Learn Regular Expressions by Building a Password Generator - Step 71

Hi, I would appreciate if someone could help. I feel like I’ve done what I’m meant to and the code seems to run fine but it’s not the right answer.
I’ve tried using an f string and different spacing between the words but it always comes up as wrong.
If someone could point me in the right direction that would be appreciated.

Your code so far

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
    

# User Editable Region

new_password = generate_password()
print('Generated password: ' + new_password)

# User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36

Challenge Information:

Learn Regular Expressions by Building a Password Generator - Step 71

You appear to have created this post without editing the template. Please edit your post to Tell us what’s happening in your own words.

Welcome to our community!

new_password = generate_password()
print('Generated password: ' + new_password)
  • you don’t need the blank space in the string 'Generated password: ’ after the colon,
  • the string should be added as the first argument to the print() call, and separate the two arguments between the parentheses with a comma,
  • the second argument is only the name of the already declared function in the code line above the one in question.

Thank you for your help!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.