Skip to content

Commit 4a8cab9

Browse files
Create Password_Generator.md
1 parent ed4b85e commit 4a8cab9

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Password Generator
2+
## Code
3+
4+
```
5+
import random
6+
7+
import random
8+
import string
9+
10+
def generate_password(length, complexity):
11+
"""
12+
Generate a random password.
13+
14+
Parameters:
15+
length (int): Length of the password.
16+
complexity (int): Complexity of the password. (1: Only letters, 2: Letters and digits, 3: Letters, digits, and punctuation)
17+
18+
Returns:
19+
str: Generated password.
20+
"""
21+
if complexity == 1:
22+
characters = string.ascii_letters
23+
elif complexity == 2:
24+
characters = string.ascii_letters + string.digits
25+
elif complexity == 3:
26+
characters = string.ascii_letters + string.digits + string.punctuation
27+
else:
28+
raise ValueError("Complexity must be 1, 2, or 3.")
29+
30+
password = ''.join(random.choice(characters) for i in range(length))
31+
return password
32+
33+
def main():
34+
"""
35+
Main function to get user input and generate password.
36+
"""
37+
try:
38+
length = int(input("Enter the desired password length: "))
39+
complexity = int(input("Enter the password complexity (1: Letters, 2: Letters and digits, 3: Letters, digits, and punctuation): "))
40+
41+
if length <= 0:
42+
raise ValueError("Length must be a positive integer.")
43+
44+
password = generate_password(length, complexity)
45+
print("Generated Password:", password)
46+
except ValueError as e:
47+
print("Error:", e)
48+
49+
if __name__ == "__main__":
50+
main()
51+
```
52+
53+
## Code Explanation

0 commit comments

Comments
 (0)