0

我正在尝试编写根据用户要求生成密码的密码生成器。如果用户输入超过两次 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
4

1 回答 1

1

由于您正在创建一个完整的字母表并从中随机选择字符,因此不能保证从每个组(数字、大写字母等)中至少选择一个。

您可以通过在字母表中将组分开来修复它。然后从每个中选择至少一个。最后,您应该将字符洗牌一次,以确保字母表中的顺序在生成的密码中丢失。

import random

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")
    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:
        alphabets = []   # keep a list of alphabet groups 
        if password_numbers in ("yes", "YES"):
            alphabets.append("0123456789")  # include group
        if password_uppercase in ("yes", "YES"):
            alphabets.append("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
        if password_lowercase in ("yes", "YES"):
            alphabets.append("abcdefghijklmnopqrstuvwxyz")
        if password_special in ("yes", "YES"):
            alphabets.append("!#$%&'()*+,-./:;<=>?@[\\]^_`{|}~")
        password_chars = []  # construct the password as a list
        while len(password_chars) < length:  # keep choosing until length requirement is met
            for alphabet in alphabets:  # for each group
                password_chars.append(random.choice(alphabet)) # choose a character from the group
                if len(password_chars) > length:
                    break
        random.shuffle(password_chars) # shuffle to lose the order of groups from the generated list
        return "".join(password_chars)

print(PasswordGenerator())

输出:

Welcome to Password Generator
You can create a password with capital letters, lowercase letters, numbers, and special characters.
During the password creation process, we will ask you which password you want.

Please enter the length of the password you want to create:
Note: The minimum length is 8 characters And enter Only integers
8
Answer only with yes or no.
Do you want upper case? 
yes
Answer only with yes or no.
Do you want lower case?
yes
Answer only with yes or no.
Do you want numbers?
yes
Answer only with yes or no.
Do you want Special symbols?
yes

OnWw0=]7
于 2020-04-11T17:53:06.823 回答