-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword_generator.py
More file actions
59 lines (47 loc) · 1.67 KB
/
Copy pathpassword_generator.py
File metadata and controls
59 lines (47 loc) · 1.67 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
# Password Generator
from random import choice, shuffle
import string
def password_gen(min_length, numbers=True, special_characters=True):
letters = string.ascii_letters
digits = string.digits
special = string.punctuation
characters = letters
if numbers:
characters += digits
if special_characters:
characters += special
pwd = []
has_number = False
has_special = False
while len(pwd) < min_length:
new_char = choice(characters)
# Ensure the generated character meets the required criteria
if numbers and new_char in digits:
has_number = True
if special_characters and new_char in special:
has_special = True
pwd.append(new_char)
# If the generated password is shorter than the specified length,
# add missing characters that meet the criteria
while len(pwd) < min_length:
new_char = choice(characters)
if numbers and not has_number and new_char in digits:
pwd.append(new_char)
has_number = True
if special_characters and not has_special and new_char in special:
pwd.append(new_char)
has_special = True
# Shuffle the password to ensure randomness
shuffle(pwd)
return ''.join(pwd)
try:
pwd_length = int(input('\nEnter the length of the password to be generated: '))
if pwd_length <= 3:
print("\nPlease Enter Positive Integer > 3\n")
elif pwd_length > 200:
print("\nPlease set length < 200\n")
else:
pwd = password_gen(pwd_length)
print(f'\nYour Generated Password is: {pwd}\n')
except:
print('\nPlease Enter Positive Integer > 3\n')