Generating strong and unique passwords is essential for maintaining security in today’s digital landscape. To make this process more accessible and user-friendly, we can create a Python script that not only generates passwords based on user preferences but also provides a clear and interactive interface. In this example, we’ll build a command-line tool that asks the user for input on password length and the inclusion of various character types, then generates a secure password accordingly.
import random
import string
def generate_password(length, use_uppercase, use_digits, use_punctuation):
characters = string.ascii_lowercase
if use_uppercase:
characters += string.ascii_uppercase
if use_digits:
characters += string.digits
if use_punctuation:
characters += string.punctuation
if not characters:
raise ValueError("At least one character set must be selected")
password = ''.join(random.choice(characters) for i in range(length))
return password
def main():
print("Welcome to the Password Generator!")
while True:
try:
length = int(input("Enter the desired password length (e.g., 12): "))
if length <= 0:
print("Length must be a positive number.")
continue
except ValueError:
print("Invalid input. Please enter a number.")
continue
use_uppercase = input("Include uppercase letters? (yes/no): ").strip().lower() == 'yes'
use_digits = input("Include digits? (yes/no): ").strip().lower() == 'yes'
use_punctuation = input("Include special characters? (yes/no): ").strip().lower() == 'yes'
try:
password = generate_password(length, use_uppercase, use_digits, use_punctuation)
print(f"Generated password: {password}")
except ValueError as e:
print(e)
another = input("Generate another password? (yes/no): ").strip().lower()
if another != 'yes':
print("Thank you for using the Password Generator. Goodbye!")
break
if __name__ == "__main__":
main()
import random
import string
def generate_password(length, use_uppercase, use_digits, use_punctuation):
characters = string.ascii_lowercase
if use_uppercase:
characters += string.ascii_uppercase
if use_digits:
characters += string.digits
if use_punctuation:
characters += string.punctuation
if not characters:
raise ValueError("At least one character set must be selected")
password = ''.join(random.choice(characters) for i in range(length))
return password
generate_password(length, use_uppercase, use_digits, use_punctuation)
characters = string.ascii_lowercase
if use_uppercase: characters += string.ascii_uppercase
if use_digits: characters += string.digits
if use_punctuation: characters += string.punctuation
characters
is empty, meaning no character sets were selected. If so, raise a ValueError
.''.join()
to generate a random password of the specified length from the selected characters.def main():
print("Welcome to the Password Generator!")
while True:
try:
length = int(input("Enter the desired password length (e.g., 12): "))
if length <= 0:
print("Length must be a positive number.")
continue
except ValueError:
print("Invalid input. Please enter a number.")
continue
use_uppercase = input("Include uppercase letters? (yes/no): ").strip().lower() == 'yes'
use_digits = input("Include digits? (yes/no): ").strip().lower() == 'yes'
use_punctuation = input("Include special characters? (yes/no): ").strip().lower() == 'yes'
try:
password = generate_password(length, use_uppercase, use_digits, use_punctuation)
print(f"Generated password: {password}")
except ValueError as e:
print(e)
another = input("Generate another password? (yes/no): ").strip().lower()
if another != 'yes':
print("Thank you for using the Password Generator. Goodbye!")
break
while True:
allows the user to generate multiple passwords without restarting the program.length = int(input("Enter the desired password length (e.g., 12): "))
generate_password()
with the specified parameters.ValueError
raised (e.g., if no character sets are selected).if __name__ == "__main__":
main()
main()
function is called only when the script is run directly, not when imported as a module.