Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Password Generator
- # Mike Kerry - 25-Jan-2021 - acclivity2@gmail.com
- # Modified April 2021
- import string # This module provides a convenient way of accessing the alphabet etc.
- import random # This module is required for the "choices()" method we will use
- def make_password(length):
- # Force "length" to be between 8 and 12 characters
- # This is a neat Pythonic approach, rather than a naive if/else test
- length = max(length, 8)
- length = min(length, 12)
- # Now we will create our password, firstly as a list
- # Start with 2 uppercase letters chosen at random using choices()
- password = random.choices(string.ascii_uppercase, k=2)
- # Then add 2 random lowercase letters
- password.extend(random.choices(string.ascii_lowercase, k=2))
- # Must include 2 digits
- password.extend(random.choices(string.digits, k=2))
- # Must include 2 "punctuation" characters
- password.extend(random.choices(string.punctuation, k=2))
- # The ppassword now is of length 8. If we want it longer we can extend it
- # with any combination of letters digits and punctuation
- if length > 8:
- mixture = string.ascii_letters + string.digits + string.punctuation
- password.extend(random.choices(mixture, k=length-8))
- # Shuffle the list
- random.shuffle(password)
- # Convert the list to a string and return it
- return "".join(password)
- # Generate some sample passwords of length 3 to 14, (these lengths will get constrained to 8 through 12)
- for x in range(12):
- print(make_password(3 + x))
- # Results
- # X0F3rb|#
- # iLF2@?5f
- # ,0Q,rU5q
- # iD8G(m=9
- # 4p/9L}Yx
- # L+aw13P|
- # <k31BNn,#
- # 69a!4%OAfc
- # fH[61uXX!d'
- # A%TcN6a3Z;[Q
- # b!oACdeS[+88
- # H4xzA,V0\uE>
Add Comment
Please, Sign In to add comment