Skip to content

Commit 456dfc0

Browse files
authored
Added comments to Password Generator
1 parent 57b1c77 commit 456dfc0

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

Password Generator.py

+13
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,54 @@
1+
# Import required modules
12
import random
23
import string
34

5+
# Define function to generate password
46
def generate_password(min_length, numbers=True, special_characters=True):
7+
# Define character sets to be used for generating password
58
letters = string.ascii_letters
69
digits = string.digits
710
special = string.punctuation
811

12+
# Determine which character sets to use based on user input
913
characters = letters
1014
if numbers:
1115
characters += digits
1216
if special_characters:
1317
characters += special
1418

19+
# Initialize variables for password generation and criteria checking
1520
pwd = ""
1621
meets_criteria = False
1722
has_number = False
1823
has_special = False
1924

25+
# Loop until password meets criteria and is long enough
2026
while not meets_criteria or len(pwd) < min_length:
27+
# Generate a new random character and add it to the password
2128
new_char = random.choice(characters)
2229
pwd += new_char
2330

31+
# Check if the new character is a number or special character
2432
if new_char in digits:
2533
has_number = True
2634
elif new_char in special:
2735
has_special = True
2836

37+
# Determine if the password meets the specified criteria
2938
meets_criteria = True
3039
if numbers:
3140
meets_criteria = has_number
3241
if special_characters:
3342
meets_criteria = meets_criteria and has_special
3443

44+
# Return the generated password
3545
return pwd
3646

47+
# Get user input for password criteria
3748
min_length = int(input("Specify the minimum length required for the password. "))
3849
has_number = input("Do you want the password to include numbers? (y/n)").lower() == "y"
3950
has_special = input("Do you want the password to include special characters? (y/n)").lower() == "y"
51+
52+
# Generate and print the password
4053
pwd = generate_password(min_length, has_number, has_special)
4154
print ("Here is the password generated: ", pwd)

0 commit comments

Comments
 (0)