I want to make a password generator I let user to choose upper, digits and, symbols but I have problem with Random part when my code choose a random password from phrase According to the conditions selected by the user, one of the conditions may not be fulfilled. For example, all the selected phrase may be lowercase, but the user has activated the uppercase condition
import string
import secrets
def random_password_generator(upper=True, digits=True, symbols=False, length=12):
a_l = string.ascii_lowercase
a_u = string.ascii_uppercase
phrase = a_l
if upper:
phrase += a_u
if digits:
phrase += string.digits
if symbols:
phrase += string.punctuation
password = ''.join(secrets.choice(phrase) for i in range(length))
return password
a = random_password_generator()
print(a)
secrets.choice(phrase) chooses a random character out of the phrase you give it. You perform that operation 10 times. There is no guarantee that during those 10 times, the random choice will fall in any of the categories you have added to your phrase.