猿问

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

所以我想写一个 Python 代码,为用户创建一个随机密码。它需要询问用户他们希望密码多长,并且有最少的字符集。


几个星期以来,我一直在努力寻找一些东西,但就是找不到,如果这个问题已经解决了,我深表歉意。


这是我到目前为止的代码,不知道我需要什么其他代码以及我必须在哪里使用才能获得我想要的东西。


import random

import secrets

import string



# This variable includes numbers

digits = string.digits


# This variable includes uppercase and lowercase letters

letters = string.ascii_lowercase


# 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?")

    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_pass



message()


慕斯王
浏览 127回答 4
4回答

繁星coding

用这个改变你的 userGen() :def userGen():&nbsp; &nbsp; c = 0&nbsp; &nbsp; upper = int(input("How many letters you want? "))&nbsp; &nbsp; spec = int(input("How many symbols you want? "))&nbsp; &nbsp; num = int(input("How many numbers you want? "))&nbsp; &nbsp; c += upper + num +spec&nbsp; &nbsp; if c < 7:&nbsp; &nbsp; &nbsp; &nbsp; print('Your password is too short.')&nbsp; &nbsp; &nbsp; &nbsp; userGen()&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; return passGen(upper,spec,num)编辑:import randomimport stringimport secretslength = 7# This variable includes numbersdigits = "1234567890"# This variable includes uppercase and lowercase lettersletters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"# This variable includes symbolssymbols = "!#$%&'()*+, -./:;<=>?@[\]^_`{|}~"password = ''.join(secrets.choice(digits + letters + symbols) for t in range(20))def message():&nbsp; &nbsp; print("Hello, here is a password generator. ")&nbsp; &nbsp; print("Choose the length of your characters:")&nbsp; &nbsp; passGen()def passGen():&nbsp; &nbsp; c = 0&nbsp; &nbsp; upper = int(input("How many letters you want? "))&nbsp; &nbsp; spec = int(input("How many symbols you want? "))&nbsp; &nbsp; num = int(input("How many numbers you want? "))&nbsp; &nbsp; c += upper + num +spec&nbsp; &nbsp; if c < length:&nbsp; &nbsp; &nbsp; &nbsp; print('Your password is too short.')&nbsp; &nbsp; &nbsp; &nbsp; passGen()&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; new_password = ""&nbsp; &nbsp; &nbsp; &nbsp; for i in range(upper):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new_password += random.choice(letters)&nbsp; &nbsp; &nbsp; &nbsp; for x in range(spec):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new_password += random.choice(symbols)&nbsp; &nbsp; &nbsp; &nbsp; for y in range(num):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new_password += random.choice(digits)&nbsp; &nbsp; &nbsp; &nbsp; pass_word = list(new_password)&nbsp; &nbsp; &nbsp; &nbsp; shuff = random.shuffle(pass_word)&nbsp; &nbsp; &nbsp; &nbsp; new_pass = "".join(pass_word)&nbsp; &nbsp; &nbsp; &nbsp; password = secrets.token_urlsafe(7)&nbsp; &nbsp; &nbsp; &nbsp; print(new_pass)message()

红颜莎娜

我相信你想要完成的事情可以用一个简单的if语句来完成。你想要:检查用户是否输入了最多 7 个字符的组合如果那是真的:像您目前正在做的那样分配密码如果不是这样,您至少有两个选择:像@Ktoto 建议的那样打印一条错误消息通过自己设置字母、数字和符号的数量来强制密码至少为 7 个字符。

江户川乱折腾

您可以像这样使用 if 语句:def userGen():&nbsp; &nbsp; upper = int(input("How many letters you want? "))&nbsp; &nbsp; spec = int(input("How many symbols you want? "))&nbsp; &nbsp; num = int(input("How many numbers you want? "))&nbsp; &nbsp; if int(upper) + int(spec) + int(num) <= 7:&nbsp; &nbsp; &nbsp; &nbsp; # your code&nbsp; &nbsp; return passGen(upper, spec, num)然后向您喜欢的特定类型的字符添加更多字符。

忽然笑

如果用户的选择加起来不够,这个问题并不清楚应该添加什么样的字符,所以我建议你只是让用户再试一次,直到有足够的为止。虽然严格来说不是问题的答案,但这里有一些关于您可以执行的代码一般整理的建议。它包括两个字典的使用:characters一个保存字符的类型,另一个choices保存用户对每种类型项目的数量的选择。如果您想更改内容,例如将“字母”拆分为大写和小写的单独计数,那么您只需要更改顶部的字典。import randomcharacters = {'digits': '1234567890',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'letters': 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'symbols': "!#$%&'()*+,-./:;<=>?@[\]^_`{|}~"}def getChoices(min_total=7):&nbsp; &nbsp; print('Hello, here is a password generator: ')&nbsp; &nbsp; print('Choose the length of your characters?')&nbsp; &nbsp; while True:&nbsp; &nbsp; &nbsp; &nbsp; total = 0&nbsp; &nbsp; &nbsp; &nbsp; choices = {}&nbsp; &nbsp; &nbsp; &nbsp; for category in characters.keys():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; number = int(input(f'How many {category} do you want? '))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; choices[f'num_{category}'] = number&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; total += number&nbsp; &nbsp; &nbsp; &nbsp; if total >= min_total:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return choices&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(f'Need at least {min_total} characters - please try again')def passGen(choices):&nbsp; &nbsp; new_password = ''&nbsp; &nbsp; for category, options in characters.items():&nbsp; &nbsp; &nbsp; &nbsp; for i in range(choices[f'num_{category}']):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new_password += random.choice(options)&nbsp; &nbsp; pass_word = list(new_password)&nbsp; &nbsp; random.shuffle(pass_word)&nbsp; &nbsp; new_pass = ''.join(pass_word)&nbsp; &nbsp; return new_passdef main():&nbsp; &nbsp; choices = getChoices()&nbsp; &nbsp; print(f'Your choices: {choices}')&nbsp; &nbsp; password = passGen(choices)&nbsp; &nbsp; print(f'Your password: {password}')main()
随时随地看视频慕课网APP

相关分类

Python
我要回答