Python program: Generates a random password based on user preferences

generates a random password

import random
import string

def generate_password(length, include_digits=True, include_symbols=True):
characters = string.ascii_letters
if include_digits:
characters += string.digits
if include_symbols:
characters += string.punctuation

password = ”.join(random.choice(characters) for _ in range(length))
return password

def main():
print(“Welcome to the Random Password Generator!”)
print(“—————————————-“)

length = int(input(“Enter the desired length of the password: “))
include_digits = input(“Include digits? (y/n): “).lower() == ‘y’
include_symbols = input(“Include symbols? (y/n): “).lower() == ‘y’

password = generate_password(length, include_digits, include_symbols)
print(“Your randomly generated password is: ” + password)

if __name__ == ‘__main__’:
main()

In this program, the generate_password function generates a random password based on the provided length and user preferences. It uses the random module to select characters randomly from a string that includes letters, digits, and symbols. The main function prompts the user to input the desired password length, whether to include digits, and whether to include symbols. It then calls the generate_password function with the user’s preferences and prints the generated password.

When you run the program, it will ask for the password length, whether to include digits, and whether to include symbols. After gathering the necessary information, it will generate a random password and display it on the screen. You can customize the program further by adding error handling or incorporating additional password complexity rules.

Top online courses in Teaching & Academics

Related Posts

Leave a Reply