1
+ import random
2
+ import string
3
+
4
+ # Write a password generator in Python. Be creative with how you
5
+ # 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
+ # Extra:
10
+ # Ask the user how strong they want their password to be.
11
+ # For weak passwords, pick a word or two from a list.
12
+
13
+ def weak (): #pick from word list
14
+ return random .choice (['strange' ,'disgusting' ,'chivalrous' ,'decide' ,'loud' ,'vivacious' ,'love' ,'toothpaste' ,'steal' ,'defeated' ,'wood' ,'claim' ])
15
+
16
+ def passgen (strength ): #generator
17
+ '''
18
+ Takes in strength as an int defining how many characters to generate for the password.
19
+ Returns a result containing the defined number of psuedo-randomly generated characters.
20
+ '''
21
+ password = ''
22
+ max = 2
23
+ for i in range (strength ):
24
+ chartype = random .randint (0 ,max ) #0 = num, 1 = letter, 2 = specialchar
25
+ if chartype == 0 :
26
+ password += random .choice (string .digits ) #this was randint(0,9), but when I was looking for the letters and punctuation, I found this, so
27
+ elif chartype == 1 :
28
+ password += random .choice (string .ascii_letters )
29
+ else :
30
+ password += random .choice (string .punctuation )
31
+ max = 1 #I got tired of all special chars
32
+ return password
33
+
34
+ # apparently this is a python main function
35
+ if __name__ == '__main__' :
36
+ # I looked it up, apparently __name__ is a variable
37
+ # the file has set to its name, until it's executed,
38
+ # then its name is '__main__', so this here is asking
39
+ # if the file is being executed, essentially. Smart
40
+
41
+ stronglength = 16
42
+ mediumlength = 8
43
+
44
+ #first, ask for strength
45
+ #Two methods, weak and other:
46
+ # weak will pick from word list
47
+ # other has random gen, which picks length depending on med or strong
48
+ strength = input ("What strength do you want your password to be? (S)trong, (M)edium, (W)eak\n " ).lower ()
49
+ while (strength != "s" and strength != "m" and strength != "w" ):
50
+ strength = input ("Please type s for strong, m for medium, or w for weak.\n " ).lower ()
51
+
52
+ if (strength == "w" ):
53
+ print ("Your weak password is: " + weak ())
54
+ elif (strength == "m" ):
55
+ print ("Your medium password is: " + passgen (mediumlength ))
56
+ else :
57
+ print ("Your strong password is: " + passgen (stronglength ))
0 commit comments