{}
run-icon
main.py
import random import string def generate_password(length, include_uppercase=True, include_digits=True, include_special=True): if length < 1: raise ValueError("Password length must be at least 1") characters = string.ascii_lowercase password = [] if include_uppercase: characters += string.ascii_uppercase password.append(random.choice(string.ascii_uppercase)) if include_digits: characters += string.digits password.append(random.choice(string.digits)) if include_special: characters += string.punctuation password.append(random.choice(string.punctuation)) while len(password) < length: password.append(random.choice(characters)) random.shuffle(password) return ''.join(password[:length]) def get_user_input(): while True: try: length = int(input("Enter the desired length of the password: ")) if length < 1: raise ValueError("Length must be a positive integer.") break except ValueError as e: print(e) include_uppercase = input("Include uppercase letters? (yes/no): ").lower() == 'yes' include_digits = input("Include digits? (yes/no): ").lower() == 'yes' include_special = input("Include special characters? (yes/no): ").lower() == 'yes' return length, include_uppercase, include_digits, include_special def main(): print("Welcome to the Password Generator!") length, include_uppercase, include_digits, include_special = get_user_input() password = generate_password(length, include_uppercase, include_digits, include_special) print(f"Generated Password: {password}") if __name__ == "__main__": main()
Output