Problem-
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.
Solution-
import random
import array
MAX_LEN = int(input("Enter the length of the Password you want (minimum length should be 6): "))
NODigits = int(input("Enter the number of digits you want in Password: "))
NOLocase = int(input("Enter the number of Lower Case you want in Password: "))
NOUpcase = int(input("Enter the number of Upper Case you want in Password: "))
NOSymbol = int(input("Enter the number of symbols you want in Password: "))
DIGITS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
LOCASE_CHARACTERS = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z']
UPCASE_CHARACTERS = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'p',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z']
SYMBOLS = ['@', '#', '$', '%', '=', ':', '?', '.', '/', '|', '~', '>',
'*', '(', ')', '<', '&']
COMBINED_LIST = DIGITS + UPCASE_CHARACTERS + LOCASE_CHARACTERS + SYMBOLS
rand_digit = random.choice(DIGITS)
rand_upper = random.choice(UPCASE_CHARACTERS)
rand_lower = random.choice(LOCASE_CHARACTERS)
rand_symbol = random.choice(SYMBOLS)
i = 1
while(i < NODigits):
rand_digit = rand_digit + random.choice(DIGITS)
i = i + 1
i = 1
while(i < NOLocase):
rand_upper = rand_upper + random.choice(UPCASE_CHARACTERS)
i = i + 1
i = 1
while(i < NOUpcase):
rand_lower = rand_lower + random.choice(LOCASE_CHARACTERS)
i = i + 1
i = 1
while(i < NOSymbol):
rand_symbol = rand_symbol + random.choice(SYMBOLS)
i = i + 1
temp_pass = rand_digit + rand_upper + rand_lower + rand_symbol
for x in range(MAX_LEN - (NODigits + NOLocase + NOUpcase + NOSymbol)):
temp_pass = temp_pass + random.choice(COMBINED_LIST)
temp_pass_list = array.array('u', temp_pass)
random.shuffle(temp_pass_list)
password = ""for x in temp_pass_list:
password = password + x
print("\nGenerated Password: ",password)OUTPUT:
Enter the length of the Password you want (minimum length should be 6): 16
Enter the number of digits you want in Password: 4
Enter the number of Lower Case you want in Password: 4
Enter the number of Upper Case you want in Password: 4
Enter the number of symbols you want in Password: 4
Generated Password: ~08=H2R>kCb$kAx0