我正在尝试编写根据用户要求生成密码的密码生成器。如果用户输入超过两次 yes 并不总是有效。并非所有要求都包含在函数创建的密码中。我很想知道如何解决这个问题并得到解释。
from random import randint
from secrets import choice
def PasswordGenerator():
print("Welcome to Password Generator\nYou can create a password with capital letters, lowercase letters, numbers, and special characters.\nDuring the password creation process, we will ask you which password you want.\n")
while True:
try:
length = int(input("Please enter the length of the password you want to create:\nNote: The minimum length is 8 characters And enter Only integers\n"))
while length < 8:
print("The minimum password length is 8 characters, please enter a valid integer number ")
length = int(input("Enter the length of the password you want to create:\n"))
password_uppercase = input("Answer only with yes or no.\nDo you want upper case? \n")
while password_uppercase != "yes" and password_uppercase != "YES" and password_uppercase != "no" and password_uppercase != "NO":
password_uppercase = input("Answer only with yes or no.\nDo you want upper case? \n")
password_lowercase = input("Answer only with yes or no.\nDo you want lower case?\n")
while password_lowercase != "yes" and password_lowercase != "YES" and password_lowercase != "no" and password_lowercase != "NO":
password_lowercase = input("Answer only with yes or no.\nDo you want lower case?\n")
password_numbers = input("Answer only with yes or no.\nDo you want numbers?\n")
while password_numbers != "yes" and password_numbers != "YES" and password_numbers != "no" and password_numbers != "NO":
password_numbers = input("Answer only with yes or no.\nDo you want numbers?\n")
password_special = input("Answer only with yes or no.\nDo you want Special symbols?\n")
while password_special != "yes" and password_special != "YES" and password_special != "no" and password_special != "NO":
password_special = input("Answer only with yes or no.\nDo you want Special symbols?\n")
except ValueError:
print("Enter Only integers")
except:
print("Oops you got an error! Please enter a VALID value")
else:
alphabet = ''
if password_numbers == "yes":
alphabet += "0123456789"
elif password_numbers == "YES":
alphabet += "0123456789"
if password_uppercase == "yes":
alphabet += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
elif password_uppercase == "YES":
alphabet += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
if password_lowercase == "yes":
alphabet += "abcdefghijklmnopqrstuvwxyz"
elif password_lowercase == "YES":
alphabet += "abcdefghijklmnopqrstuvwxyz"
if password_special == "yes":
alphabet += "!#$%&'()*+,-./:;<=>?@[\]^_`{|}~"
elif password_special == "YES":
alphabet += "!#$%&'()*+,-./:;<=>?@[\]^_`{|}~"
password = "".join(choice(alphabet) for x in range(randint(length, length)))
return password
break