Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6cb0eda

Browse files
authored
Create Exercise-16-Password-Generator.py
1 parent 2e9adcc commit 6cb0eda

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
'''
2+
Exercise 16: Password Generator
3+
4+
Write a password generator in Python. Be creative with how
5+
you generate passwords - strong passwords have a mix of lowercase
6+
letters, uppercase letters, numbers, and symbols. The passwords
7+
should be random, generating a new password every time the user
8+
asks for a new password. Include your run-time code in a main method.
9+
10+
Extra:
11+
12+
Ask the user how strong they want their password to be. For weak
13+
passwords, pick a word or two from a list.
14+
15+
'''
16+
17+
# Solution
18+
def generate_passwords(level):
19+
"""
20+
Generate password of specific level.
21+
22+
Arguments:
23+
level -- password security level. 8 characters for low, 12 for
24+
middle, and 16 for high. In each pattern, the password is a
25+
mix of lowercase letters, uppercase letters, numbers, and symbols.
26+
27+
Returns:
28+
password -- a password string.
29+
"""
30+
import string
31+
import random
32+
33+
level_dict = {'low':8, 'middle':12, 'high':16}
34+
35+
segment = [0, 0, 0, 0]
36+
for x in range(level_dict[level]):
37+
r = random.randint(0,3)
38+
segment[r] += 1
39+
40+
lower_letter = random.choices(string.ascii_lowercase, k=segment[0])
41+
upper_letter = random.choices(string.ascii_uppercase, k=segment[1])
42+
digits = random.choices(string.digits, k=segment[2])
43+
symbols = random.choices('!@#$%^&*()_+', k=segment[3])
44+
45+
password = lower_letter + upper_letter + digits + symbols
46+
random.shuffle(password)
47+
48+
return ''.join(password)
49+
50+
def check_level_input(string, level):
51+
"""
52+
Check the input string is a level or not.
53+
54+
Arguments:
55+
string -- input string.
56+
level -- correct level string.
57+
58+
Returns:
59+
True/False -- if the input is correct level, return True, else return false.
60+
"""
61+
if string.lower() == level or string.lower() == level.lower()[0]:
62+
return True
63+
else:
64+
return False
65+
66+
def main():
67+
while True:
68+
level = input('Choose your password security level: [low/middle/high]')
69+
if level.lower() == 'exit':
70+
break
71+
elif check_level_input(level, 'low'):
72+
print(generate_passwords('low'))
73+
elif check_level_input(level, 'middle'):
74+
print(generate_passwords('middle'))
75+
elif check_level_input(level, 'high'):
76+
print(generate_passwords('high'))
77+
else:
78+
print('Input error.')
79+
80+
if __name__ == "__main__":
81+
main()
82+
83+
# Test Part
84+
# Choose your password security level: [low/middle/high]low
85+
# #*ug38Gu
86+
# Choose your password security level: [low/middle/high]mi
87+
# Input error.
88+
# Choose your password security level: [low/middle/high]middle
89+
# N*(&wB_If1#R
90+
# Choose your password security level: [low/middle/high]high
91+
# _)6oC03@y%BJj)u9
92+
# Choose your password security level: [low/middle/high]high
93+
# jEby&f8$^59z9l%K
94+
# Choose your password security level: [low/middle/high]exit

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /