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

密码生成器 - 嵌套 while 循环

密码生成器 - 嵌套 while 循环

皈依舞 2022-12-06 17:27:29
我正在研究下面的密码生成器和一个让我困惑的问题。有一个嵌套的 while 循环询问用户是否愿意在生成密码后选择一个新密码。每次您选择“是”时,理想情况下它应该生成一个新密码来替换旧密码。事实并非如此,每次重新选择时,它都会在旧密码上添加一个新密码。我不在这里做什么?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贡献1886条经验 获得超2个赞

选择“Y”后并没有停止循环,因此未完成的函数将继续执行。通过使用return语句(而不是break),您可以停止函数打印它生成的密码。


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())

作为旁注,如果您实际使用它们,则不应使用默认的随机模块来生成密码。使用该secrets模块或使用随机数生成器播种更安全


import os

import random

random.seed(os.urandom(16))

实现安全。


查看完整回答
反对 回复 2022-12-06
?
慕虎7371278

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

您可以添加一个标志而不是再次调用该函数(内部 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


查看完整回答
反对 回复 2022-12-06
  • 2 回答
  • 0 关注
  • 160 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号