为了账号安全,请及时绑定邮箱和手机立即绑定

如何设置创建密码生成器并询问密码应该多长?

如何设置创建密码生成器并询问密码应该多长?

慕斯王 2023-06-20 10:23:11
所以我想写一个 Python 代码,为用户创建一个随机密码。它需要询问用户他们希望密码多长,并且有最少的字符集。几个星期以来,我一直在努力寻找一些东西,但就是找不到,如果这个问题已经解决了,我深表歉意。这是我到目前为止的代码,不知道我需要什么其他代码以及我必须在哪里使用才能获得我想要的东西。import randomimport secretsimport string# This variable includes numbersdigits = string.digits# This variable includes uppercase and lowercase lettersletters = string.ascii_lowercase# This variable includes symbolssymbols = "!#$%&'()*+, -./:;<=>?@[\]^_`{|}~"password = ''.join(secrets.choice(digits + letters + symbols) for t in range(20))def message():    print("Hello, here is a password generator: ")    print("Choose the length?")    print(userGen())def userGen():    upper = int(input("How many letters you want? "))    spec = int(input("How many symbols you want? "))    num = int(input("How many numbers you want? "))    return passGen(upper,spec,num)def passGen(upper,spec,num):    new_password = ""    for i in range(upper):        new_password += random.choice(letters)    for x in range(spec):        new_password += random.choice(symbols)    for y in range(num):        new_password += random.choice(digits)    pass_word = list(new_password)    shuff = random.shuffle(pass_word)    new_pass = "".join(pass_word)    password = secrets.token_urlsafe(7)    return new_passmessage()
查看完整描述

4 回答

?
繁星coding

TA贡献1797条经验 获得超4个赞

用这个改变你的 userGen() :


def userGen():

    c = 0

    upper = int(input("How many letters you want? "))

    spec = int(input("How many symbols you want? "))

    num = int(input("How many numbers you want? "))

    c += upper + num +spec

    if c < 7:

        print('Your password is too short.')

        userGen()

    else:

        return passGen(upper,spec,num)

编辑:


import random

import string

import secrets


length = 7



# This variable includes numbers

digits = "1234567890"


# This variable includes uppercase and lowercase letters

letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"


# This variable includes symbols

symbols = "!#$%&'()*+, -./:;<=>?@[\]^_`{|}~"


password = ''.join(secrets.choice(digits + letters + symbols) for t in range(20))

def message():

    print("Hello, here is a password generator. ")

    print("Choose the length of your characters:")

    passGen()


def passGen():

    c = 0

    upper = int(input("How many letters you want? "))

    spec = int(input("How many symbols you want? "))

    num = int(input("How many numbers you want? "))

    c += upper + num +spec

    if c < length:

        print('Your password is too short.')

        passGen()

    else:

        new_password = ""

        for i in range(upper):

            new_password += random.choice(letters)

        for x in range(spec):

            new_password += random.choice(symbols)

        for y in range(num):

            new_password += random.choice(digits)


        pass_word = list(new_password)

        shuff = random.shuffle(pass_word)

        new_pass = "".join(pass_word)

        password = secrets.token_urlsafe(7)

        print(new_pass)


message()


查看完整回答
反对 回复 2023-06-20
?
红颜莎娜

TA贡献1842条经验 获得超12个赞

我相信你想要完成的事情可以用一个简单的if语句来完成。你想要:

  • 检查用户是否输入了最多 7 个字符的组合

  • 如果那是真的:

    • 像您目前正在做的那样分配密码

  • 如果不是这样,您至少有两个选择:

    • 像@Ktoto 建议的那样打印一条错误消息

    • 通过自己设置字母、数字和符号的数量来强制密码至少为 7 个字符。


查看完整回答
反对 回复 2023-06-20
?
江户川乱折腾

TA贡献1851条经验 获得超5个赞

您可以像这样使用 if 语句:


def userGen():

    upper = int(input("How many letters you want? "))

    spec = int(input("How many symbols you want? "))

    num = int(input("How many numbers you want? "))

    if int(upper) + int(spec) + int(num) <= 7:

        # your code

    return passGen(upper, spec, num)

然后向您喜欢的特定类型的字符添加更多字符。


查看完整回答
反对 回复 2023-06-20
?
忽然笑

TA贡献1806条经验 获得超5个赞

如果用户的选择加起来不够,这个问题并不清楚应该添加什么样的字符,所以我建议你只是让用户再试一次,直到有足够的为止。


虽然严格来说不是问题的答案,但这里有一些关于您可以执行的代码一般整理的建议。它包括两个字典的使用:characters一个保存字符的类型,另一个choices保存用户对每种类型项目的数量的选择。如果您想更改内容,例如将“字母”拆分为大写和小写的单独计数,那么您只需要更改顶部的字典。


import random


characters = {'digits': '1234567890',

              'letters': 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',

              'symbols': "!#$%&'()*+,-./:;<=>?@[\]^_`{|}~"}



def getChoices(min_total=7):

    print('Hello, here is a password generator: ')

    print('Choose the length of your characters?')

    while True:

        total = 0

        choices = {}

        for category in characters.keys():

            number = int(input(f'How many {category} do you want? '))

            choices[f'num_{category}'] = number

            total += number

        if total >= min_total:

            return choices

        else:

            print(f'Need at least {min_total} characters - please try again')



def passGen(choices):

    new_password = ''

    for category, options in characters.items():

        for i in range(choices[f'num_{category}']):

            new_password += random.choice(options)

    pass_word = list(new_password)

    random.shuffle(pass_word)

    new_pass = ''.join(pass_word)

    return new_pass



def main():

    choices = getChoices()

    print(f'Your choices: {choices}')

    password = passGen(choices)

    print(f'Your password: {password}')



main()



查看完整回答
反对 回复 2023-06-20
  • 4 回答
  • 0 关注
  • 126 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信