make an advanced password generator in python

To make an advanced password generator in Python, you can use the built-in random module to generate random passwords. Here's an example program that generates a password of a specified length and complexity:

main.py
import random
import string

def generate_password(length=8, complexity="medium"):
    """Generate a random password of a given length and complexity."""
    
    # Define the character sets for each complexity level
    lower = string.ascii_lowercase
    upper = string.ascii_uppercase
    digits = string.digits
    punct = string.punctuation
    
    # Choose the character set based on the complexity level
    if complexity == "low":
        charset = lower
    elif complexity == "high":
        charset = lower + upper + digits + punct
    else:
        charset = lower + upper + digits
    
    # Generate the password
    password = "".join(random.choice(charset) for i in range(length))
    
    return password
722 chars
25 lines

This function takes two arguments: length and complexity. length specifies the length of the generated password (default is 8), and complexity specifies the complexity level of the generated password (low, medium, or high; default is medium).

The function first defines the character sets for each complexity level using string.ascii_lowercase, string.ascii_uppercase, string.digits, and string.punctuation. It then chooses the appropriate character set based on the complexity argument.

Finally, the function generates the password by choosing a random character from the character set length times using a list comprehension, and then joining the resulting characters into a string.

Here's an example usage of the function:

main.py
password = generate_password(length=12, complexity="high")
print(password)  # Outputs something like "#kLJ2@a$7^Gc"
116 chars
3 lines

This will generate a password of length 12 with high complexity. The output password will be a random combination of uppercase letters, lowercase letters, digits, and punctuation.

gistlibby LogSnag