|
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 | 1 | """ |
| 2 | +Password generation program that allows symbols and capital letters |
| 3 | +based on user preferences. |
7 | 4 |
|
8 | | - |
| 5 | +Returns: |
| 6 | + str: Generated password |
| 7 | +""" |
9 | 8 |
|
10 | 9 | import random |
11 | 10 |
|
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 |
| 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 |
24 | 35 | if capitals: |
25 | | - characters+=CAPITAL_LETTERS |
| 36 | + characters += CAPITAL_LETTERS |
| 37 | + |
| 38 | + # Add symbols if requested |
26 | 39 | if symbols: |
27 | | - characters+=SYMBOLS |
28 | | - password="" |
29 | | - for i in range(0,passlength): |
30 | | - randomletter=random.choice(characters) |
31 | | - password+=(randomletter) |
| 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 |
32 | 47 |
|
33 | 48 | return password |
34 | 49 |
|
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 | 50 |
|
43 | | - # 1. Validate both inputs at once |
| 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 |
44 | 65 | if input_capitals not in ['y', 'n'] or input_symbols not in ['y', 'n']: |
45 | 66 | 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') |
| 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') |
51 | 72 |
|
| 73 | + # 3. Validate password length |
52 | 74 | if pass_length.isdigit(): |
53 | | - #validate password length |
54 | | - length=int(pass_length) |
55 | | - if 12<=length<=24: |
56 | | - return length,capitals,symbols |
| 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 |
57 | 80 | else: |
58 | 81 | print("Please enter password length within the range!!") |
59 | 82 | continue |
60 | | - print("Password length should be a Number") |
61 | | - |
62 | | - |
63 | | - |
| 83 | + |
| 84 | + print("Password length should be a number") |
64 | 85 |
|
65 | 86 |
|
66 | 87 | def main(): |
67 | | - length,capitals,symbols=inputs_validation() |
68 | | - password=dynamic_password(length,capitals,symbols) |
69 | | - print(f"Your Generated Password is : {password}") |
| 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}") |
70 | 97 |
|
71 | 98 |
|
72 | 99 | if __name__ == "__main__": |
|
0 commit comments