-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPasswordGenerator.py
42 lines (34 loc) · 1.4 KB
/
PasswordGenerator.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
import string
import random
# Function to get user input and validate it is a non-negative number
def get_user_input(prompt):
while True:
try:
user_input = int(input(prompt))
if user_input < 0:
print("Your number should be non-negative.")
else:
return user_input
except ValueError:
print("Please enter numbers only.")
print("Welcome to Password Generator")
# Ask user about the number of characters
num_letters = get_user_input("How many letters do you want in your password? ")
num_numbers = get_user_input("How many numbers do you want in your password? ")
num_symbols = get_user_input("How many symbols do you want in your password? ")
# Using some predefined functions from the respective library
letters_upper = string.ascii_uppercase
letters_lower = string.ascii_lowercase
numbers = string.digits
symbols = string.punctuation
# Using random.choices() to allow repetition
letters = random.choices(letters_upper + letters_lower, k=num_letters)
nums = random.choices(numbers, k=num_numbers)
syms = random.choices(symbols, k=num_symbols)
# Concatenate the lists and shuffle them
password_list = letters + nums + syms
random.shuffle(password_list)
# Join the shuffled list elements to create the password
password = "".join(password_list)
# Output the generated password
print('This is your password requirements:', password)