-
Notifications
You must be signed in to change notification settings - Fork 0
/
New password generator app using python
44 lines (37 loc) · 1.57 KB
/
New password generator app using python
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
#New password generator app using python
import random
import string
def generate_password(length, include_lowercase=True, include_uppercase=True,
include_digits=True, include_punctuation=True):
characters = ''
if include_lowercase:
characters += string.ascii_lowercase
if include_uppercase:
characters += string.ascii_uppercase
if include_digits:
characters += string.digits
if include_punctuation:
characters += string.punctuation
if not characters:
raise ValueError('No character types selected for password generation.')
password = ''.join(random.choice(characters) for _ in range(length))
return password
if __name__ == '__main__':
while True:
try:
length = int(input('Enter the desired length of the password: '))
if length <= 0:
raise ValueError
break
except ValueError:
print('Invalid input. Please enter a positive integer.')
include_lowercase = input('Include lowercase letters? (y/n): ').lower() == 'y'
include_uppercase = input('Include uppercase letters? (y/n): ').lower() == 'y'
include_digits = input('Include digits? (y/n): ').lower() == 'y'
include_punctuation = input('Include punctuation? (y/n): ').lower() == 'y'
try:
password = generate_password(length, include_lowercase, include_uppercase,
include_digits, include_punctuation)
print('Generated Password:', password)
except ValueError as e:
print('Error:', str(e))