I made this random password script. It works, but i think it is kind of bloated. The part where random characters are being added is the area where I think it needs work. I don't want to declare if and else statement for every combination of lowercase list, uppercase list and digits list(I am taking input from user whether they want uppercase,lowercase,digits or any possible combination of these three). Is there any possible way in which I can just check which of these three values are yes and then just use only those.
Also what is more efficient using lower() to turn the list called using string module into lower case or just calling the lowercase list itself.
import random
from string import ascii_letters ,digits
while True:
userchoice_size = int(input("Enter the length of the password : ")) # Asks for length of password
userchoice_syntax_lower = input("Do you want lower case letter?(y or n) ")
userchoice_syntax_upper = input("Do you want upper case letter?(y or n) ")
userchoice_syntax_number = input("Do you want numbers?(y or n) ")
counter=0
password='' # String which stores the password
choice_list=[1,2,3]
while counter <= userchoice_size:
choice_number = random.choice(choice_list)
if choice_number == 1:
if userchoice_syntax_upper == 'y': # Uppercase list
password = password + str(random.choice(ascii_letters.upper()))
counter += 1
if choice_number == 2:
if userchoice_syntax_number=='y': # Digits list
password = password + str(random.choice(digits))
counter += 1
if choice_number == 3:
if userchoice_syntax_lower == 'y': # Lower case list
password= password + str(random.choice(ascii_letters.lower())) # Converts all letters into lowercase then randomly select and store in list
counter += 1
print (password)