1+ """
2+ Password generation program that allows symbols and capital letters
3+ based on user preferences.
4+
5+ Returns:
6+ str: Generated password
7+ """
8+
9+ import random
10+
11+ # Constants
12+ SMALL_LETTERS = "abcdefghijklmnopqrstuvwxyz"
13+ CAPITAL_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
14+ SYMBOLS = "~!@#$%^&*()_-+={[}]|:;<>?"
15+ MIN_LENGTH = 12
16+ MAX_LENGTH = 24
17+
18+
19+ def dynamic_password (passlength , capitals = False , symbols = False ):
20+ """
21+ Generate a random password based on specified criteria.
22+
23+ Args:
24+ passlength (int): Length of the password
25+ capitals (bool): Whether to include capital letters
26+ symbols (bool): Whether to include symbols
27+
28+ Returns:
29+ str: Generated password
30+ """
31+ # Start with small letters as the base character set
32+ characters = SMALL_LETTERS
33+
34+ # Add capital letters if requested
35+ if capitals :
36+ characters += CAPITAL_LETTERS
37+
38+ # Add symbols if requested
39+ if symbols :
40+ characters += SYMBOLS
41+
42+ # Generate password by randomly selecting characters
43+ password = ""
44+ for i in range (0 , passlength ):
45+ random_letter = random .choice (characters )
46+ password += random_letter
47+
48+ return password
49+
50+
51+ def inputs_validation ():
52+ """
53+ Get and validate user inputs for password generation.
54+
55+ Returns:
56+ tuple: (length, capitals, symbols) - validated user preferences
57+ """
58+ while True :
59+ # Get user inputs
60+ pass_length = input (f"Enter password length ({ MIN_LENGTH } -{ MAX_LENGTH } ): " )
61+ input_capitals = input ("Do we include capitals (y/n): " ).strip ().lower ()
62+ input_symbols = input ("Do we include symbols (y/n): " ).strip ().lower ()
63+
64+ # 1. Validate both yes/no inputs at once
65+ if input_capitals not in ['y' , 'n' ] or input_symbols not in ['y' , 'n' ]:
66+ print ("Please type 'y' or 'n' for the options." )
67+ continue # Restart the loop to ask again
68+
69+ # 2. Convert inputs to booleans
70+ capitals = (input_capitals == 'y' )
71+ symbols = (input_symbols == 'y' )
72+
73+ # 3. Validate password length
74+ if pass_length .isdigit ():
75+ length = int (pass_length )
76+
77+ # Check if length is within valid range
78+ if MIN_LENGTH <= length <= MAX_LENGTH :
79+ return length , capitals , symbols
80+ else :
81+ print ("Please enter password length within the range!!" )
82+ continue
83+
84+ print ("Password length should be a number" )
85+
86+
87+ def main ():
88+ """Main function to orchestrate the password generation process."""
89+ # Get validated user inputs
90+ length , capitals , symbols = inputs_validation ()
91+
92+ # Generate password
93+ password = dynamic_password (length , capitals , symbols )
94+
95+ # Display the generated password
96+ print (f"Your Generated Password is: { password } " )
97+
98+
99+ if __name__ == "__main__" :
100+ main ()
0 commit comments