PASSWORD GENERATOR USING PYTHON
Program :
import random
import string
lower = string.ascii_lowercase # abcdefghijklmnopqrstuvwxyz
upper = string.ascii_uppercase # ABCDEFGHIJKLMNOPQRSTUVWXYZ
letters = string.ascii_letters # abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
punc = string.punctuation # !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
digits = string.digits # 0123456789
def password(length, num=False, strength="risky"):
"""length -> no of characters,
num -> if True returns password with numbers,
strength -> risky, weak, strong"""
pwd = ""
# If the value of default variable strength is 'risky'
if strength == "risky":
if num:
length = length - 2
for i in range(2):
pwd = pwd + random.choice(digits)
for i in range(length):
pwd = pwd + random.choice(lower)
# If the value of default variable strength is 'weak'
elif strength == "weak":
if num:
length = length - 2
for i in range(2):
pwd = pwd + random.choice(digits)
length = length - 2
for i in range(2):
pwd = pwd + random.choice(upper)
for i in range(length):
pwd = pwd + random.choice(lower)
# If the value of default variable strength is 'strong'
elif strength == "strong":
if length < 7:
print("To get very strong password, length must be greater than 7")
else:
if num:
length = length - 3
for i in range(3):
pwd = pwd + random.choice(digits)
length = length - 2
for i in range(2):
pwd = pwd + random.choice(upper)
length = length - 1
for i in range(1):
pwd = pwd + random.choice(punc)
for i in range(length):
pwd = pwd + random.choice(lower)
# print(pwd)
pwd = list(pwd)
# print(pwd)
random.shuffle(pwd)
pwd = ''.join(pwd)
return pwd
length = int(input("Enter length: "))
print("Risky Password")
print(password(length))
print(password(length, num=True))
print()
print("Weak Password")
print(password(length, strength="weak"))
print(password(length, num=True, strength="weak"))
print()
print("Strong Password")
print(password(length, strength="strong"))
print(password(length, num=True, strength="strong"))
print()
Output :
Case 1 :
Case 2 :
Also Refer : For loop patterns using Python.
Comments