Skip to content

Instantly share code, notes, and snippets.

@carrickkv2
Created December 7, 2021 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carrickkv2/e47e6245a2427a47b96a90e720effa86 to your computer and use it in GitHub Desktop.
Save carrickkv2/e47e6245a2427a47b96a90e720effa86 to your computer and use it in GitHub Desktop.
import secrets
import string
def generate_random_password(num: int = 8, symbols: str = None, numbers: str = None, uppercase: str = None, lowercase: str = None) -> str:
"""
This function generates a random password that has at least 8 characters.
The password can be customised by the user.
"""
symbol = string.punctuation
number = string.digits
upper = string.ascii_uppercase
lower = string.ascii_lowercase
password = ""
if symbols == 'yes':
password += symbol
if numbers == 'yes':
password += number
if uppercase == 'yes':
password += upper
if lowercase == 'yes':
password += lower
return ''.join((secrets.choice(password) for i in range(num))) if password else ''.join((secrets.choice(string.ascii_letters + string.digits + string.punctuation) for i in range(num)))
while True:
try:
length = int(input(
"\nPlease enter the length of password. Note that it should be greater than 7 \n"))
if length > 7:
break
except ValueError:
print("You must enter a number")
symbols = input(
"\nPlease type yes if you'll like to include symbols: ").lower()
numbers = input(
"\nPlease type yes if you'll like to include numbers: ").lower()
uppercase = input(
"\nPlease type yes if you'll like to include uppercase characters: ").lower()
lowercase = input(
"\nPlease type yes if you'll like to include lowercase characters: ").lower()
if __name__ == "__main__":
print(
f"\nThis is your password \n{generate_random_password(length, symbols, numbers, uppercase, lowercase)}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment