Skip to content

Commit 31aceba

Browse files
committed
formatted-code
1 parent d44802a commit 31aceba

1 file changed

Lines changed: 75 additions & 48 deletions

File tree

Password-Generator/dynamic_password.py

Lines changed: 75 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,99 @@
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
61
"""
2+
Password generation program that allows symbols and capital letters
3+
based on user preferences.
74
8-
5+
Returns:
6+
str: Generated password
7+
"""
98

109
import random
1110

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
2435
if capitals:
25-
characters+=CAPITAL_LETTERS
36+
characters += CAPITAL_LETTERS
37+
38+
# Add symbols if requested
2639
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
3247

3348
return password
3449

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()
4250

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
4465
if input_capitals not in ['y', 'n'] or input_symbols not in ['y', 'n']:
4566
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')
5172

73+
# 3. Validate password length
5274
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
5780
else:
5881
print("Please enter password length within the range!!")
5982
continue
60-
print("Password length should be a Number")
61-
62-
63-
83+
84+
print("Password length should be a number")
6485

6586

6687
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}")
7097

7198

7299
if __name__ == "__main__":

0 commit comments

Comments
 (0)