-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword_generator.py
103 lines (81 loc) · 3.36 KB
/
password_generator.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
def password_generator():
"""
Returns a random password value containing alpha, numeric, and symbol characters
Parameters:
length (int): Desired length of password
Output:
password (str): New random password
"""
# import modules
import string
import random
# declare variables
available_characters = ''
password_list = []
password = ''
# ask for user parameters
print("This program will allow you to create a complex new password. Please answer the following questions to begin.")
print('')
length = int(input("How many characters do you need in your password? Please limit your length to 50 characters or less. "))
letters = input("Would you like to include letters in your password? (Y/N) ")
numbers = input("How about numbers? (Y/N) ")
symbols = input("Should we include special characters? (Y/N) ")
# generate available characters
if letters.upper() == 'Y':
if numbers.upper() == 'Y':
if symbols.upper() == 'Y':
available_characters = string.ascii_letters + string.digits + string.punctuation
elif symbols.upper() == 'N':
available_characters = string.ascii_letters + string.digits
else:
available_characters = 'error'
elif numbers.upper() == 'N':
if symbols.upper() == 'Y':
available_characters = string.ascii_letters + string.punctuation
elif symbols.upper() == 'N':
available_characters = string.ascii_letters
else:
available_characters = 'error'
elif letters.upper() == 'N':
if numbers.upper() == 'Y':
if symbols.upper() == 'Y':
available_characters = string.digits + string.punctuation
elif symbols.upper() == 'N':
available_characters = string.digits
else:
available_characters = 'error'
elif numbers.upper() == 'N':
if symbols.upper() == 'Y':
available_characters = string.punctuation
elif symbols.upper() == 'N':
available_characters = 'no_values'
else:
available_characters = 'error'
else:
available_characters = 'error'
else:
available_characters = 'error'
# identify potential errors with user-input length
if isinstance(length, int) is False:
length = 'error'
elif length > 50:
length = 'error'
elif length < 1:
length = 'error'
# communicate error / processing messages
if available_characters == 'error' or length == 'error':
return "Looks like you didn't enter valid responses. Please try again."
elif available_characters == 'no_values':
return "Unable to generate a password. You must choose to include at least one of the following options: letters, numbers, or special characters."
else:
print("Thanks for the help!")
print("Generating complex password now...")
print("----------------------------------")
# calculate password in list format
password_list = random.sample(str(available_characters), length)
# convert list to string output
for x in password_list:
password += x
# return password
return "Your new password: " + str(password)
print(password_generator())