-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrand_pass2.py
58 lines (46 loc) · 1.77 KB
/
rand_pass2.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
import random
import string
def generate_password(length, use_letters=True, use_digits=True, use_special=True):
characters = ""
if use_letters:
characters += string.ascii_letters
if use_digits:
characters += string.digits
if use_special:
characters += string.punctuation
if not characters:
raise ValueError("No character sets selected for password generation.")
return ''.join(random.choice(characters) for _ in range(length))
def tree_menu():
print("\n--- Random Password Generator ---")
while True:
print("\nChoose an option:")
print("1. Generate password")
print("2. Exit")
choice = input("Enter your choice: ").strip()
if choice == "1":
password_config()
elif choice == "2":
print("Exiting. Goodbye!")
break
else:
print("Invalid choice. Please try again.")
def password_config():
try:
length = int(input("Enter password length: ").strip())
if length <= 0:
print("Password length must be greater than zero.")
return
print("\nInclude the following in your password:")
use_letters = input("Letters (y/n): ").strip().lower() == 'y'
use_digits = input("Digits (y/n): ").strip().lower() == 'y'
use_special = input("Special characters (y/n): ").strip().lower() == 'y'
if not (use_letters or use_digits or use_special):
print("You must include at least one character type!")
return
password = generate_password(length, use_letters, use_digits, use_special)
print("\nGenerated Password:", password)
except ValueError as e:
print(f"Error: {e}. Please try again.")
if __name__ == "__main__":
tree_menu()