亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何設置創建密碼生成器并詢問密碼應該多長?

如何設置創建密碼生成器并詢問密碼應該多長?

慕斯王 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條經驗 獲得超13個贊

我相信你想要完成的事情可以用一個簡單的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 關注
  • 175 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號