Learn Regular Expressions by Building a Password Generator - Step 62

Tell us what’s happening:

Describe your issue in detail here.

Hi team. I need help to solve this step. Here is my code so far.
Preformatted text

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 */

        all([constraint <= len(re.findall(pattern, password))
        for constraint, pattern in constraints]):

/* 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 Edg/120.0.0.0

Challenge Information:

Learn Regular Expressions by Building a Password Generator - Step 62

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.

Hello Brice,

Very close! But right the question at the beginning said:

Modify your if condition

I don’t see an if statement anymore. (As a hint, you are to check if all elements are True).

2 Likes

@HungryBee
I think I follow what it is that you are saying.
Take the If and place it around the all() function and within the all() is contains all the conditions.
I am still stuck and can’t get it to run.
see code below:


if all (constraint <= len(re.findall(pattern, password))
            for constraint, pattern in constraints):

Is there a glaring mistake I am making here.
THank you in advance.

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

The if statement condition has to have a specific character to conclude it, do you remember what it is?

Thank you for the formatting correction.

All I can think of a colon for the end of an if statement. :thinking:

that can be the answer, yes, do you have it in your code?

This is the updated code I have tried. It is still not working. I am certain the mistake is something small and silly.

if [all (constraint <= len(re.findall(pattern, password))
            for constraint, pattern in constraints)]:
                break

P.S. is this the correct formatting?

I don’t see you adding a colon at the end of the if statement condition

what are those square brackets you added?

Those square brackets were just an attempt to try somthing different.

These are the versions of the code I have tried so far focussing on trying to complete the if statement.

if (all (constraint <= len(re.findall(pattern, password))
            for constraint, pattern in constraints):):
            break
if all (constraint <= len(re.findall(pattern, password))
            for constraint, pattern in constraints)::
            break

I can see I need a colon at the end of the statement for 'if ’ and the ‘all()’ statements and functions, respectively. Am I missing something really silly here?

this is your if statement condition, it needs a colon at the end
all() doesn’t need a colon at the end
the for loop is indise the if statement but is not part of the if statement condition

1 Like

Ok perfect, thank you that makes sense to me now.

However, the code is still not working.
I have tried resetting the code block and puting the code in again.

if all (constraint <= len(re.findall(pattern, password))):
            for constraint, pattern in constraints:
                break

That is my updated code. My question now is. Is the ‘break’ still required? Would it not be implemented on the first iteration through the for loop?

I solved it by jumping ahead to the next stage and looking at the code there.
Here are some pointer for you if you do want to stick to the learning and solving fun.

Place the all() function within the if statement:
I see it as a if all ( …conditions etc… ):
Keep the [ ]'s around the whole argument both the test for <= and for loop.

Hope that helps.