-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPassword Generator.py
96 lines (75 loc) · 3.03 KB
/
Password Generator.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import random
def checkalpha(password):
''' Returns True if a uppercase and lowercase alpahabetic charather is contained in password'''
letuppercase=False
letlowercase=False
for x in password:
if(x.isupper()):
letuppercase=True
if(x.islower()):
letlowercase=True
if(letuppercase and letlowercase):
return True
else:
return False
def checknum(password):
'''Returns True if a number is contained in the password'''
for x in password:
if(x.isdigit()):
return True
return False
def checkchar(password):
'''Returns True if a special character is contained in the password'''
for x in password:
if(x == '.' or "," or "/" or '?' or '>' or '<' or x == "'" or x == ':' or x == ';' or x == '!' or x == '@' or x == '#' or x == '$' or x == '%' or x == '^' or x == '&' or x == '*' or x == '(' or x == ')' or x == '-' or x == '+' or x == '=' or x == '_' or x == '[' or x == ']' or x == '{' or x == '}'):
return True
return False
def checkpassword(password):
'''Returns True if the password contains a specail character,number and alpahabetical letter'''
if(checkalpha(password) and checknum(password) and checkchar(password)):
return True
else:
return False
def getchar():
'''Returns a random Uppercase or Lowercase character'''
chars=['a','b','c','d','e','f','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
random.shuffle(chars)
toreturn = chars[random.randint(0,24)]
if(random.randint(1,2)==2):
return toreturn.upper()
else:
return toreturn
def getspecialchar():
'''Returns a special character'''
specialchars=['.',',',"'",'<','>','/','?',';',':',"'",'[',']','{','}','!','@','#',"$",'%','^','&','*','(',')','-','_','+','=']
random.shuffle(specialchars)
return specialchars[random.randint(0,27)]
def makepassword(length):
'''Returns a password'''
password=[]
choices=['c','n','s']
random.shuffle(choices)
for let in range(0,length):
choiceindex=random.randint(0,2)
if(choices[choiceindex]=='c'):
password.append(getchar())
elif(choices[choiceindex]=='n'):
password.append(str(random.randint(0,9)))
else:
password.append(getspecialchar())
return ''.join(password)
def main():
'''Main program'''
print('Welcome to the password generator! How long of a password do you want?')
length = int(input())
if(length<1):
print('You can\'t do that! Try entering a number greater than 1')
main()
while(True):
currentpassword=makepassword(length)
if(checkpassword(currentpassword)):
break
else:
continue
print("Your password is \n{}".format(currentpassword))
main()