Write a programme, which generates a random password for the user. Ask the user how long they want their password to be, and how many letters and numbers they want in their password. Have a mix of upper and lowercase letters, as well as numbers and symbols. The password should be a minimum of 6 characters long.
import string import random import sys # Taking Password Length pass_len = int(input("Enter Password Length (Minimum 6 and Maximum 24 Characters): ")) # Checking for password is in range or not if pass_len < 6 or pass_len > 24: print("Invalid password length!!, Minimum 6 and Maximum 24 Characters Allowed") sys.exit(0) # Password's letters count letters_len = int(input("How many letters do you want? (Maximum {} and Minimum 0): ".format(pass_len))) # Password's numbers count num_len = int(input("How many numbers do you want? (Maximum {} and Minimum 0): ".format(pass_len-letters_len))) # Checking for valid inputs if pass_len < (letters_len+num_len): print("Invalid inputs!! Letters length plus numbers length exceed total password length.") sys.exit(0) sp_sym = 'Y' if pass_len != (letters_len+num_len): sp_sym = input("Do you want to include special symbols? (Y for Yes, N for No): ") # Checking for valid character if sp_sym not in ['Y','y','N','n']: print("Wrong Character!! Y/y for Yes and N/n for No ") sys.exit(0) # Password without special symbols if sp_sym == 'N' or sp_sym == 'n': print("Invalid inputs!! Letters length plus numbers length is not equal to password length.") sys.exit(0) # Password with special symbols if sp_sym == 'Y' or sp_sym == 'y': passw = random.sample(string.ascii_letters,letters_len) + random.sample(string.digits,num_len) \ + random.sample(string.punctuation,pass_len-(letters_len+num_len)) else: passw = random.sample(string.ascii_letters+string.digits,pass_len) # Shuffle random character's list random.shuffle(passw) # List to string conversion pass_gen = ''.join(passw) print("Your Password: ",pass_gen) # Techhighland™
OUTPUT
Enter Password Length (Minimum 6 and Maximum 24 Characters): 11
How many letters do you want? (Maximum 11 and Minimum 0): 5
How many numbers do you want? (Maximum 6 and Minimum 0): 4
Do you want to include special symbols? (Y for Yes, N for No): y
Your Password: 6k1FeD&9t\8
No comments:
Post a Comment