def generatePassword(length): characters = string.ascii_letters + string.digits + string.punctuation while True: # Keep generating until the password meets the criteria password = ''.join(random.choice(characters) for i in range(length)) if (any(c.islower() for c in password) # At least one lowercase letter and any(c.isupper() for c in password) # At least one uppercase letter and any(c.isdigit() for c in password) # At least one digit and any(c in string.punctuation for c in password)): # At least one special character return password
while True: length = int(input("Enter a password length (or 0 to quit): ")) if length == 0: break elif length < 4: # Ensure the length is enough to meet all criteria print("Password length must be at least 4 to meet all criteria.") else: print(f'Generated password: {generatePassword(length)}')
import random
import string
def generate_pasword(length):
characters = string.ascii_letters + string.digits + string.punctuation
password = ''.join(random.choice(characters) for _ in range(length))
return password
while True:
length = int(input("Enter a password length (or 0 to exit): "))
if length == 0:
break
print(f"Generated password: {generate_pasword(length)}")
You are awesome for showing us this project. Thanks for the video!
@@eddiemeddie2018 my pleasure; thanks for the comment!
def generatePassword(length):
characters = string.ascii_letters + string.digits + string.punctuation
while True: # Keep generating until the password meets the criteria
password = ''.join(random.choice(characters) for i in range(length))
if (any(c.islower() for c in password) # At least one lowercase letter
and any(c.isupper() for c in password) # At least one uppercase letter
and any(c.isdigit() for c in password) # At least one digit
and any(c in string.punctuation for c in password)): # At least one special character
return password
while True:
length = int(input("Enter a password length (or 0 to quit): "))
if length == 0:
break
elif length < 4: # Ensure the length is enough to meet all criteria
print("Password length must be at least 4 to meet all criteria.")
else:
print(f'Generated password: {generatePassword(length)}')
very nice way to add on to the project!