-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path014-random_password.py
More file actions
executable file
·44 lines (33 loc) · 1.19 KB
/
014-random_password.py
File metadata and controls
executable file
·44 lines (33 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python3
#Random Password Generator
import random, string
#Step 1: Define a password generation function
def generate_password(length = 12):
if length < 4:
raise ValueError("Password length must be at least 4 characters")
#Character sets for the password
uppercase = string.ascii_uppercase
lowercase = string.ascii_lowercase
digits = string.digits
special_chars = "!@#$%^&*()_+[]{}:;',.<>"
#Ensure at least one of each character type
password = [
random.choice(uppercase),
random.choice(lowercase),
random.choice(digits),
random.choice(special_chars)
]
#Fill the remaining length with random choices from all sets
all_chars = uppercase + lowercase + digits + special_chars
password += random.choices(all_chars, k = length - 4)
#Shuffle the password to make it more random
random.shuffle(password)
#Convert the list to a string and return
return "".join(password)
#Step 2: User Interaction
try:
length = int(input("Enter the desired password length (minimun 4): "))
password = generate_password(length)
print("Generated Password: ", password)
except ValueError as e:
print(e)