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)