Learn Regular Expressions by Building a Password Generator - Step 66

I’ve run this back (as you can tell by the comment out) with the general number inputs and it works fine. However, once I name the variables and assign it errors out for some reason.

I’ve gone through this up and down making sure my inputs match and everything. Maybe I need extra eyes to tell me what I’m doing wrong. Anyone see anything?

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'),
            (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(length = 8, nums = 1, special_cars = 1, uppercase = 1, lowercase = 1)
#new_password = generate_password(8,1,1,1,1)
print(new_password)

The argument should be special_chars. Missing the h

It is not enforced but good practice not to have spaces between variable and value when given as an argument.
Example:
Instead of nums = 1 do nums=1

Oh? It should be referencing the special_chars from the functions original arguments

oh wait nevermind I see! It was a typo with the A on your response, I have cars and not chars

1 Like

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