Learn Regular Expressions by Building a Password Generator - Step 68

Tell us what’s happening:

Describe your issue in detail here.

Your code so far

import re
import secrets
import string


# User Editable Region

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

# User Editable Region

    # 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
    

new_password = generate_password(nums=1, length=8, special_chars=1, uppercase=1, lowercase=1)
print(new_password)

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2.1 Safari/605.1.15

Challenge Information:

Learn Regular Expressions by Building a Password Generator - Step 68

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.
Learning to describe problems is hard, but it is an important part of learning how to code.
Also, the more you say, the more we can help!

I have added values to the parameters (length = "16, nums = “1”, etc) I am not sure why this is not passing.

Hello @mjb2024

In the request it says:

Modify your function declaration to take default parameters. Use 16 for the length and 1 for the other constraints.

Perhaps it is not clear that those values are not strings but integers. What’s the difference? The quotes.
Also, in Python it is considered best practice not to place spaces between variable names and values in function arguments.
Instead of this (var = 1) do (var=1)

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