Skip to content

Commit 7eb89c3

Browse files
Merge pull request souravjain540#135 from derco19/main
12 digit Password Generator with constraints of special_chars and digits
2 parents 9488683 + 1290b93 commit 7eb89c3

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

random_password_generator.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import secrets
2+
import string
3+
4+
letters = string.ascii_letters
5+
digits = string.digits
6+
special_chars = string.punctuation
7+
8+
alphabet = letters + digits + special_chars
9+
10+
# fix password length
11+
pwd_length = 12
12+
13+
# generate password meeting constraints
14+
while True:
15+
pwd = ''
16+
for i in range(pwd_length):
17+
pwd += ''.join(secrets.choice(alphabet))
18+
19+
if (any(char in special_chars for char in pwd) and
20+
sum(char in digits for char in pwd) >= 2):
21+
break
22+
print(pwd)

0 commit comments

Comments
 (0)