第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

密碼生成器 - 嵌套 while 循環(huán)

密碼生成器 - 嵌套 while 循環(huán)

皈依舞 2022-12-06 17:27:29
我正在研究下面的密碼生成器和一個(gè)讓我困惑的問(wèn)題。有一個(gè)嵌套的 while 循環(huán)詢問(wèn)用戶是否愿意在生成密碼后選擇一個(gè)新密碼。每次您選擇“是”時(shí),理想情況下它應(yīng)該生成一個(gè)新密碼來(lái)替換舊密碼。事實(shí)并非如此,每次重新選擇時(shí),它都會(huì)在舊密碼上添加一個(gè)新密碼。我不在這里做什么?import randomfrom string import ascii_lowercase, ascii_uppercase, digitsspecial_chars = "!#$%&*@^"available_chars = list(ascii_lowercase) + list(ascii_uppercase) + list(digits) + list(special_chars)def get_length():    user_l = ""    while not user_l.isdigit() or int(user_l) < 6:        user_l = input("Please input a password length.\n")    return int(user_l)    print(int(user_l))def pwd_gen(length):    return [random.choice(available_chars) for i in range(length)]def pwd_chk(length):    pwd = []    while True:        pwd = pwd_gen(length)        if set(pwd) & set(ascii_lowercase) == set():            continue        elif set(pwd) & set(ascii_uppercase) == set():            continue        elif set(pwd) & set(digits) == set():            continue        elif set(pwd) & set(special_chars) == set():            continue        else:            print("\nYour password is " + "".join(pwd))            while True:                accept = input("Would you like a different password? Y/N\n\n")                if accept.lower() == "n" or accept.lower() == "no":                    print("\nYour password is:")                    break                elif accept.lower() == "y" or accept.lower() == "yes":                    pwd_chk(length)                    break                else:                    print("\nInvalid input. Your password is " + "".join(pwd))                    continue            break    pwd = "".join(pwd)    print(pwd)pwd_chk(get_length())
查看完整描述

2 回答

?
MM們

TA貢獻(xiàn)1886條經(jīng)驗(yàn) 獲得超2個(gè)贊

選擇“Y”后并沒(méi)有停止循環(huán),因此未完成的函數(shù)將繼續(xù)執(zhí)行。通過(guò)使用return語(yǔ)句(而不是break),您可以停止函數(shù)打印它生成的密碼。


import random

from string import ascii_lowercase, ascii_uppercase, digits


special_chars = "!#$%&*@^"

available_chars = list(ascii_lowercase) + list(ascii_uppercase) + list(digits) + list(special_chars)



def get_length():

    user_l = ""

    while not user_l.isdigit() or int(user_l) < 6:

        user_l = input("Please input a password length.\n")

    return int(user_l)

    print(int(user_l))


def pwd_gen(length):

    return [random.choice(available_chars) for i in range(length)]



def pwd_chk(length):

    pwd = []

    while True:

        pwd = pwd_gen(length)

        if set(pwd) & set(ascii_lowercase) == set():

            continue

        elif set(pwd) & set(ascii_uppercase) == set():

            continue

        elif set(pwd) & set(digits) == set():

            continue

        elif set(pwd) & set(special_chars) == set():

            continue

        else:

            print("\nYour password is " + "".join(pwd))

            while True:

                accept = input("Would you like a different password? Y/N\n\n")

                if accept.lower() == "n" or accept.lower() == "no":

                    print("\nYour password is:")

                    break

                elif accept.lower() == "y" or accept.lower() == "yes":

                    pwd_chk(length)

                    return # CHANGE THIS FROM "break" TO "return"! #

                else:

                    print("\nInvalid input. Your password is " + "".join(pwd))

                    continue

            break


    pwd = "".join(pwd)

    print(pwd)


pwd_chk(get_length())

作為旁注,如果您實(shí)際使用它們,則不應(yīng)使用默認(rèn)的隨機(jī)模塊來(lái)生成密碼。使用該secrets模塊或使用隨機(jī)數(shù)生成器播種更安全


import os

import random

random.seed(os.urandom(16))

實(shí)現(xiàn)安全。


查看完整回答
反對(duì) 回復(fù) 2022-12-06
?
慕虎7371278

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超4個(gè)贊

您可以添加一個(gè)標(biāo)志而不是再次調(diào)用該函數(shù)(內(nèi)部 while 代碼):


            while True:

                replace_password = False # Flag added here

                accept = input("Would you like a different password? Y/N\n\n")

                if accept.lower() == "n" or accept.lower() == "no":

                    print("\nYour password is:")

                    break

                elif accept.lower() == "y" or accept.lower() == "yes":

                    replace_password = True # Setting flag

                    break

                else:

                    print("\nInvalid input. Your password is " + "".join(pwd))

                    continue

            if replace_password: # Action based on flag

                continue

            break


查看完整回答
反對(duì) 回復(fù) 2022-12-06
  • 2 回答
  • 0 關(guān)注
  • 152 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)