【问题标题】:Python password generator problem according to the user requirement根据用户要求的Python密码生成器问题
【发布时间】:2020-07-24 10:09:56
【问题描述】:

我正在尝试编写密码生成器 谁根据用户要求生成密码。 如果用户输入超过两次是 它并不总是有效。 并非所有要求都包含在函数创建的密码中。 我很想知道如何解决这个问题并得到解释。

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

【问题讨论】:

  • 不应该只通过... for x in range(length)for x in range(randint(length, length)))
  • 此外,您永远无法确保在每个类别中至少选择了一个角色。你必须改变你的方法,或者在生成密码后检查它是否符合你的条件,如果不符合,重试直到它符合。

标签: python python-3.x passwords password-generator


【解决方案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

【讨论】:

  • 感谢您的详细解答。我注意到,如果我请求长度为 10,它会返回长度为 12 的密码。我还尝试在除了 ValueError 和 while true 之后返回“尝试”,但它不起作用。
  • 对。密码长度将是字母组数的倍数。我已经改变了答案以适应这一点。对于 ValueError 部分,我建议将输入部分与实际密码计算分开。您可以在while循环中输入并在输入部分捕获ValueErrors,密码构造部分应保持不变。
猜你喜欢
  • 2021-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-18
  • 2022-11-22
  • 2021-07-05
  • 2020-04-17
  • 1970-01-01
相关资源
最近更新 更多