Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions password_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import random
import string

def generate_password(length=12, use_special_chars=True):
"""Generate a random password.

Args:
length (int): Length of the password.
use_special_chars (bool): Whether to include special characters.

Returns:
str: Generated password.
"""
if length < 4:
raise ValueError("Password length must be at least 4 characters.")

characters = string.ascii_letters + string.digits
if use_special_chars:
characters += string.punctuation

password = ''.join(random.choice(characters) for _ in range(length))
return password