Skip to content

Instantly share code, notes, and snippets.

@nitishk72
Created October 3, 2017 14:59
Show Gist options
  • Save nitishk72/b6c88f924fa737120fe6c51f1d0a561e to your computer and use it in GitHub Desktop.
Save nitishk72/b6c88f924fa737120fe6c51f1d0a561e to your computer and use it in GitHub Desktop.
# Random password generator
import random, re, string
def validate(password):
# password validation
# a letter
letter = re.search(r"[a-zA-Z]",
password) is None
# a digit
digit = re.search(r"\d", password) is None
# a symbol
symbol = re.search(r"\W", password) is None
return not(letter or digit or symbol)
def random_password(length):
charset = (
string.ascii_letters
+ string.digits
+ string.punctuation
)
return ''.join((
random.choice(charset)
for ch in range(length)
))
def main():
try:
# Enter the length of password to be generated
passwd_len = int(input())
except:
print("Please enter a number!")
return 1
if passwd_len >= 3 and passwd_len <= 100:
password = ""
while not validate(password):
password = random_password(
passwd_len
)
print("Your new password is ready :")
print("-"*30)
print(password)
else:
print("Please enter a number between 3 and 100.")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment