-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsw_gen.py
66 lines (51 loc) · 2.21 KB
/
psw_gen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import string
import random
def psw_gen(length, digits, uppercase, lowercase, symbols):
password = ""
required_characters = []
character_count = {}
if digits:
digit = random.choice(string.digits)
required_characters.append(digit)
character_count[digit] = character_count.get(digit, 0) + 1
password += string.digits
if uppercase:
upper = random.choice(string.ascii_uppercase)
required_characters.append(upper)
character_count[upper] = character_count.get(upper, 0) + 1
password += string.ascii_uppercase
if lowercase:
lower = random.choice(string.ascii_lowercase)
required_characters.append(lower)
character_count[lower] = character_count.get(lower, 0) + 1
password += string.ascii_lowercase
if symbols:
symbol = random.choice(string.punctuation)
required_characters.append(symbol)
character_count[symbol] = character_count.get(symbol, 0) + 1
password += string.punctuation
if not required_characters:
return "No characters selected to generate a password."
# Fill the rest of the password length with random characters
while len(required_characters) < length:
char = random.choice(password)
required_characters.append(char)
character_count[char] = character_count.get(char, 0) + 1
# Shuffle the password
random.shuffle(required_characters)
# Convert the list to a string
final = ''.join(required_characters)
# Print the character count for each character
print("Character counts:", character_count)
return final
# User input
length = int(input("Please enter the number of characters: "))
digits = input("Do you want your password to contain numbers? (Y/N): ").upper() == "Y"
symbols = input("Do you want your password to contain symbols? (Y/N): ").upper() == "Y"
lowercase = input("Do you want your password to include lowercase letters? (Y/N): ").upper() == "Y"
uppercase = input("Do you want your password to include uppercase letters? (Y/N): ").upper() == "Y"
# Generate password
final = psw_gen(length, digits, lowercase, uppercase, symbols)
# Print the result
if final:
print("Here's your password:", final)