Learn Regular Expressions by Building a Password Generator - Step 61

Tell us what’s happening:

I am not sure what do I need to do with the list after list comprehension and if I need to fill it with counts

Your code so far

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
        count = 0

/* User Editable Region */

        lst = [count+1 for constraint, pattern in constraints if constraint <= len(re.findall(pattern, password)) if count<=4]
        if len(lst) == 5:

        #for constraint, pattern in constraints:
        #    if constraint <= len(re.findall(pattern, password)):
        #        count += 1
            
        #if count == 4:

/* User Editable Region */

            break

    return password

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) 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 61

Hi there! I also stumbled on this one - my issue was that I didn’t understand or missed this part of the prompt:

For the if condition, use a list created with the list comprehension syntax.

What this means is that the code should read like:

if [ ... your list comprehension here ]

Hope this helps. Happy coding!

1 Like

I am this far but still doesn`t pass the test:

if 4 == [count+1 for constraint, pattern in constraints if constraint <= len(re.findall(pattern, password))]:

I don’t want to spoil the answer for you, if you want to see it, you can skip forward to Step 62 to see it. However, some more tips:

  • In a later step, you’ll delete count, so you don’t need to be referencing count here. You can achieve this task without it.
  • There shouldn’t be any text in between your “if” and list comprehension “[…”
  • You’ve already declared your if statement outside your list comprehension, you don’t need to use if again inside it
  • You have all the pieces of the answer inside your code right now, they’re just out of order and you have some extraneous stuff (the count, the if)
3 Likes

Thank you, @brittshook. It took 5 seconds to rewrite the code after your advise. I spent 3 hours yesterday trying to make it work, lol. They had to mention that count variable is not going to be used anymore.

So glad to hear that! Yeah, I think they’re trying to implicitly get at it in the:

Instead of using a loop and a counter variable, you can achieve the same result with a different approach.

But it’s really tricking, I’ve been having some issues getting through things just b/c the wording is unclear… or they want a specific variable name that they don’t mention :roll_eyes: (shout-out shortest path step 50)

2 Likes