1+ """_summary_
2+ implementing password generation program that allows symbols and capital letters if the user wants to include them
3+
4+ Returns:
5+ password
6+ """
7+
8+
9+
10+ import random
11+
12+
13+ #constants
14+ SMALL_LETTERS = "abcdefghijklmnopqrstuvwxyz"
15+ CAPITAL_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
16+ SYMBOLS = "~!@#$%^&*()_-+={[}]|:;<>?"
17+ MIN_LENGTH = 12
18+ MAX_LENGTH = 24
19+
20+
21+ #function to generate the password
22+ def dynamic_password (passlength ,capitals = False ,symbols = False ):
23+ characters = SMALL_LETTERS
24+ if capitals :
25+ characters += CAPITAL_LETTERS
26+ if symbols :
27+ characters += SYMBOLS
28+ password = ""
29+ for i in range (0 ,passlength ):
30+ randomletter = random .choice (characters )
31+ password += (randomletter )
32+
33+ return password
34+
35+ #Function to take user inputs and validate them
36+ def inputs_validation ():
37+ while True :
38+
39+ pass_length = input (f"Enter password length ({ MIN_LENGTH } -{ MAX_LENGTH } ): " )
40+ input_capitals = input ("Do we include capitals (y/n): " ).strip ().lower ()
41+ input_symbols = input ("Do we include symbols (y/n): " ).strip ().lower ()
42+
43+ # 1. Validate both inputs at once
44+ if input_capitals not in ['y' , 'n' ] or input_symbols not in ['y' , 'n' ]:
45+ print ("Please type 'y' or 'n' for the options." )
46+ continue # Restarts the loop to ask again
47+
48+ #2. convert to booleans
49+ capitals = (input_capitals == 'y' )
50+ symbols = (input_symbols == 'y' )
51+
52+ if pass_length .isdigit ():
53+ #validate password length
54+ length = int (pass_length )
55+ if 12 <= length <= 24 :
56+ return length ,capitals ,symbols
57+ else :
58+ print ("Please enter password length within the range!!" )
59+ continue
60+ print ("Password length should be a Number" )
61+
62+
63+
64+
65+
66+ def main ():
67+ length ,capitals ,symbols = inputs_validation ()
68+ password = dynamic_password (length ,capitals ,symbols )
69+ print (f"Your Generated Password is : { password } " )
70+
71+
72+ if __name__ == "__main__" :
73+ main ()
0 commit comments