尝试创建一个可以保存密码的密码生成器

问题描述 投票:0回答:1

我已经创建了程序的基线,并且运行良好,但我希望它能够保存密码生成,并将它们存储在 SQLite 中的索引名称下。我希望这样我就可以随时访问之前生成的密码。

代码:

    def gen_password():
        letters = string.ascii_letters
        random_letter = random.choice(letters)
        random_num = random.randint(0,9)
        letter_or_num = ["letter","number"]
        char_num = int(input("How many characters do you want in your password?: "))
        for i in range(char_num):
            random_var = random.choice(letter_or_num)
            if random_var == "letter":
                random_letter = random.choice(letters)
                print(random_letter, end = "")
            elif random_var == "number":
                random_num = random.randint(0,9)
                print(random_num, end = "")
        print("")
    
    gen_password()
python sqlite password-generator
1个回答
0
投票

我希望这能解决您的问题:

def gen_password():
    letters = string.ascii_letters
    random_letter = random.choice(letters)
    random_num = random.randint(0,9)
    letter_or_num = ["letter","number"]
    char_num = int(input("How many characters do you want in your password?: "))
   
    password = []
    for i in range(char_num):
        random_var = random.choice(letter_or_num)
        if random_var == "letter":
            random_letter = random.choice(letters)
            # replace print with this:
            password.append(random_letter)  
         
        elif random_var == "number":
            random_num = random.randint(0,9)
            password.append(str(random_num))
    return "".join(password)

print(gen_password())
© www.soinside.com 2019 - 2024. All rights reserved.