I am working on the below password generator and an issue that baffles me.
There's a nested while loop which asks the user if they would like to select a new password after one is generated. Each time you selected "Yes", it should ideally generate a new password to replace the old one. It is not the case, with every re-selection, it adds a new password to the old one.
What am I not doing right here?
import random
from string import ascii_lowercase, ascii_uppercase, digits
special_chars = "!#$%&*@^"
available_chars = list(ascii_lowercase) + list(ascii_uppercase) + list(digits) + list(special_chars)
def get_length():
user_l = ""
while not user_l.isdigit() or int(user_l) < 6:
user_l = input("Please input a password length.\n")
return int(user_l)
print(int(user_l))
def pwd_gen(length):
return [random.choice(available_chars) for i in range(length)]
def pwd_chk(length):
pwd = []
while True:
pwd = pwd_gen(length)
if set(pwd) & set(ascii_lowercase) == set():
continue
elif set(pwd) & set(ascii_uppercase) == set():
continue
elif set(pwd) & set(digits) == set():
continue
elif set(pwd) & set(special_chars) == set():
continue
else:
print("\nYour password is " + "".join(pwd))
while True:
accept = input("Would you like a different password? Y/N\n\n")
if accept.lower() == "n" or accept.lower() == "no":
print("\nYour password is:")
break
elif accept.lower() == "y" or accept.lower() == "yes":
pwd_chk(length)
break
else:
print("\nInvalid input. Your password is " + "".join(pwd))
continue
break
pwd = "".join(pwd)
print(pwd)
pwd_chk(get_length())
if not set(pwd) or not (set(ascii_lowercase) == set() | set(ascii_uppercase) == set() | set(digits) == set() | set(special_chars) == set()):. You can then get rid of all thecontinuestatements.