-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPass_gen_task.py
More file actions
68 lines (52 loc) · 2.07 KB
/
Pass_gen_task.py
File metadata and controls
68 lines (52 loc) · 2.07 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import random
import string
def generate_password(length, use_uppercase=True, use_digits=True, use_special=True):
lowercase = string.ascii_lowercase
uppercase = string.ascii_uppercase if use_uppercase else ''
digits = string.digits if use_digits else ''
special = string.punctuation if use_special else ''
all_chars = lowercase + uppercase + digits + special
# Ensure at least one character from each allowed set is included
password = []
if use_uppercase:
password.append(random.choice(uppercase))
if use_digits:
password.append(random.choice(digits))
if use_special:
password.append(random.choice(special))
# Fill the remaining with random characters
remaining_length = length - len(password)
for _ in range(remaining_length):
password.append(random.choice(all_chars))
# Shuffle to avoid patterns
random.shuffle(password)
return ''.join(password)
def get_user_input():
print("Password Generator")
print("----------------")
while True:
try:
length = int(input("Enter desired password length (8-128): "))
if 8 <= length <= 128:
break
else:
print("Please enter a value between 8 and 128.")
except ValueError:
print("Please enter a valid number.")
# Complexity options
print("\nPassword complexity options:")
use_uppercase = input("Include uppercase letters? (y/n): ").lower() == 'y'
use_digits = input("Include digits? (y/n): ").lower() == 'y'
use_special = input("Include special characters? (y/n): ").lower() == 'y'
return length, use_uppercase, use_digits, use_special
def main():
# Get user preferences
length, use_uppercase, use_digits, use_special = get_user_input()
# Generate password
password = generate_password(length, use_uppercase, use_digits, use_special)
# Display the password
print("\nGenerated Password:")
print("------------------")
print(password)
if __name__ == "__main__":
main()