|
| 1 | +""" |
| 2 | +Random Password Generator |
| 3 | +---------------------------------- |
| 4 | +This program generates a strong random password |
| 5 | +based on user preferences for length and character types. |
| 6 | + |
| 7 | +Concepts covered: |
| 8 | +- Using Python's 'random' and 'string' modules |
| 9 | +- Loops, conditionals, and user input handling |
| 10 | +- Building and joining strings dynamically |
| 11 | +""" |
| 12 | + |
| 13 | +# Importing the required modules |
| 14 | +import random # For generating random selections |
| 15 | +import string # For accessing predefined character sets (letters, digits, symbols) |
| 16 | + |
| 17 | + |
| 18 | +def generate_password(length, use_uppercase, use_digits, use_symbols): |
| 19 | + """ |
| 20 | + Generates a random password based on user choices. |
| 21 | + |
| 22 | + Parameters: |
| 23 | + length (int): Desired password length |
| 24 | + use_uppercase (bool): Include uppercase letters if True |
| 25 | + use_digits (bool): Include numbers if True |
| 26 | + use_symbols (bool): Include punctuation/symbols if True |
| 27 | + |
| 28 | + Returns: |
| 29 | + str: The generated password |
| 30 | + """ |
| 31 | + |
| 32 | + # Start with lowercase letters by default |
| 33 | + characters = list(string.ascii_lowercase) |
| 34 | + |
| 35 | + # Add more character sets based on user's choices |
| 36 | + if use_uppercase: |
| 37 | + characters += list(string.ascii_uppercase) |
| 38 | + |
| 39 | + if use_digits: |
| 40 | + characters += list(string.digits) |
| 41 | + |
| 42 | + if use_symbols: |
| 43 | + characters += list(string.punctuation) |
| 44 | + |
| 45 | + # If somehow no characters are chosen, return an error |
| 46 | + if not characters: |
| 47 | + return "Error: No character types selected!" |
| 48 | + |
| 49 | + # Randomly choose characters to form the password |
| 50 | + password = "".join(random.choice(characters) for _ in range(length)) |
| 51 | + |
| 52 | + return password |
| 53 | + |
| 54 | + |
| 55 | +def main(): |
| 56 | + """Main function that interacts with the user and prints the generated password.""" |
| 57 | + print("🔐 Welcome to the Random Password Generator!") |
| 58 | + print("You can customize your password strength and length.\n") |
| 59 | + |
| 60 | + # Get the desired password length |
| 61 | + try: |
| 62 | + length = int(input("Enter desired password length (e.g. 8, 12, 16): ")) |
| 63 | + if length <= 0: |
| 64 | + print("⚠️ Length must be greater than zero.") |
| 65 | + return |
| 66 | + except ValueError: |
| 67 | + print("⚠️ Please enter a valid number.") |
| 68 | + return |
| 69 | + |
| 70 | + # Ask for user preferences |
| 71 | + use_uppercase = input("Include uppercase letters? (y/n): ").lower() == "y" |
| 72 | + use_digits = input("Include digits? (y/n): ").lower() == "y" |
| 73 | + use_symbols = input("Include special symbols? (y/n): ").lower() == "y" |
| 74 | + |
| 75 | + # Generate the password |
| 76 | + password = generate_password(length, use_uppercase, use_digits, use_symbols) |
| 77 | + |
| 78 | + # Display the result |
| 79 | + print("\n✅ Your generated password is:") |
| 80 | + print(password) |
| 81 | + print("\n💡 Tip: Store it securely and don't reuse passwords across sites.") |
| 82 | + |
| 83 | + |
| 84 | +# Entry point check |
| 85 | +if __name__ == "__main__": |
| 86 | + main() |
0 commit comments